Developer forum

Forum » Development » Seeing assigned groups in UserSaved subscriber

Seeing assigned groups in UserSaved subscriber

Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi there,

I am trying to write some custom code when a user is saved using a UserSaved subscriber. It looks like when UserSaved runs the user's Groups collection isn't populated yet as Groups is empty.

I am trying to find a parent group of type SystemAccount so I can copy some data down to the user. Is there a way to get the groups or maybe another subscriber I can tap into instead?

Thanks!

Imar


Replies

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Bump. Anyone?

 
Dmitriy Benyuk Dynamicweb Employee
Dmitriy Benyuk
Reply

Hi Imar,

The prooblem is that a user and their group memberships are saved in two separate steps. UserManagementServices.Users.Save(user) writes the user record and then raises UserSaved. Group memberships are written afterwards by a separate call to UserManagementServices.Users.AddGroupRelations(user, groups), and that call needs the user to exist already. Everything that creates users does it in that order: the user modules, checkout, forms and so on. So:

  • For a new user, UserSaved always fires before any group has been assigned, so GetGroups() returns nothing.
  • For an existing user, UserSaved gives you the memberships from before the current edit.

user.Groups (now obsolete; use user.GetGroups()) holds no state of its own. It reads the memberships from the database when you call it, so it can only return what has been saved so far.

There's currently no notification for when a user is added to or removed from a group, so there isn't another subscriber you can switch to.

Workarounds

  • If your own code assigns the group, do the copy-down right after your AddGroupRelations call. At that point the relation exists and user.GetAncestorGroups() will find the SystemAccount parent.
  • If you can find the account group from user data such as the customer number, you can skip membership entirely: UserManagementServices.UserGroups.GetGroupsByCustomerNumber(user.CustomerNumber) inside UserSaved.
  • Otherwise, move the copy-down out of the save, for example into a scheduled task that syncs the fields from the parent account group to its users.

BR, Dmitrij

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Thanks Dmitrij, I think it will be option 3 because:

>> If your own code assigns the group

It doesn't. It's UI driven and the user is adding a login underneath a system account

and 

> If you can find the account group from user data such as the customer number, you can skip membership entirely

I can't. The whole purpose of this exercise is to find the customer number from the parent account and assign it to the login :-)

Imar

 

 

You must be logged in to post in the forum