User and membership api

2019-10-08 15:26:00.0
Developers

A frequent task in templating and controller development is to find what groups and organisations the current user is a member of. Kademi supports this through the Membership API.

There are three objects used to represent users

  • Profile - the domain object, which represents a user stored in the database. This is the preferred representation to work with.
  • UserResource - an addressable object (ie with its own href). Mostly deprecated.
  • ProfileBean - a lightweight object suitable for use when traversing large user lists. Mostly deprecated.

The current user is represented in templating context with the $user variable. So you can retrieve their first name like this:

$user.firstName

You can get a ProfileBean from a UserResource with the profile property. Eg:

$user.profile

From a UserResource you can also get the Profile like this:

$user.thisUser

If you have a ProfileBean and need a UserResource you can look it up as a resource by its href:

#set( $theUser = $page.find("/users/$profile.userName" ))

 

If you only have an identifier for a user you can look it up from the UserManager. User identifiers can be a Long for the Kademi internal ID or a String for the username or userid.

Eg 1 - to find by the internal ID

#set( $id = $formatter.toLong("12345") )

#set( $theUser = $services.userManager.findById($id) )

 

Eg 2 - to find by user name

#set( $userid = "userA" )

#set( $theUser = $services.userManager.findByName($userid) )

 

Eg 3 - to find by email

#set( $email = "usera@mailinator.com" )

#set( $theUser = $services.userManager.findByEmail($email) )

 

Once you have a Profile object you can get the memberships list from the UserManager

#set( $memberships = $services.userManager.membershipList($theUser) )

Note you must pass a Profile object, not a ProfileBean or UserResource.
 

 

To get the list of memberships you use the memberships property, which returns a MembershipList:

$user.memberships

That returns a list of MembershipBean, where each membership represents a link between a profile, a group and an organisation. The organisation is accessed through the org property which returns an OrgData object, which has properties and methods for accessing the organisation information and complete membership

The MembershipList can be filtered to get the list of memberships only for a certain group or organisation.

For example, to display the list of addresses of car dealerships the current user is a Dealer for you would use this:

#foreach( $membership in $memberships.filterByGroup("Dealer") )
    $membership.org.address
#end

 

The

 

 

The organisation representing the current Kademi account can be accessed through the orgData property of the WebsiteRootFolder. The current folder is in templating context as the $rootFolder variable. From the OrgData object you can lookup organisations inside it with the childOrgs() method, which returns an OrgDataList.

So to display all dealerships and the number of Dealers in them you might do this:


#foreach( $dealership in $rootFolder.orgData.childOrgs()  )
$dealership.title    $dealership.members("Dealer").size()
#end