def member_added(context, event): groupInfo = event.groupInfo userInfo = event.memberInfo siteInfo = groupInfo.siteInfo auditor = SiteMemberAuditor(context, userInfo, siteInfo) if user_member_of_site(userInfo, siteInfo.siteObj): auditor.info(JOIN_SITE_MEMBER) else: site_root = context.site_root() acl_users = getattr(site_root, 'acl_users') if not acl_users: raise ValueError('ACL Users not found in site_root') groupNames = acl_users.getGroupNames() siteInfo = groupInfo.siteInfo memberGroupId = member_id(siteInfo.id) if memberGroupId not in groupNames: m = '%s not in %s' % (memberGroupId, groupNames) raise ValueError(m) acl_users.addGroupsToUser([memberGroupId], userInfo.id) auditor.info(JOIN_SITE) notify(GSJoinSiteEvent(context, siteInfo, userInfo)) if not user_member_of_site(userInfo, siteInfo.siteObj): m = 'Tried to add {0} ({1}) to the site {2} ({3}) but they are not a member' msg = m.format(userInfo.name, userInfo.id, siteInfo.name, siteInfo.id) raise SiteAddError(msg)
def member_removed(context, event): groupInfo = event.groupInfo userInfo = event.memberInfo siteInfo = groupInfo.siteInfo auditor = SiteMemberAuditor(context, userInfo, siteInfo) if user_member_of_group_on_site(context, userInfo): auditor.info(LEAVE_SITE_MEMBER) else: site_root = context.site_root() acl_users = getattr(site_root, 'acl_users') if not acl_users: raise ValueError('ACL Users not found in site_root') memberGroupId = member_id(siteInfo.id) try: acl_users.delGroupsFromUser([memberGroupId], userInfo.id) except ValueError as ve: m = 'Tried to remove %s (%s) from the site %s (%s) but '\ 'got a ValueError:\n%s' log.warning(m, userInfo.name, userInfo.id, siteInfo.name, memberGroupId, ve) else: auditor.info(LEAVE_SITE) notify(GSLeaveSiteEvent(context, siteInfo, userInfo)) if user_member_of_site(userInfo, siteInfo.siteObj): m = 'Tried to remove {0} ({1}) from {2} ({3}) because they are no longer in any '\ 'groups, but they are still a member of the site.' msg = m.format(userInfo.name, userInfo.id, siteInfo.name, siteInfo.id) raise SiteLeaveError(msg)
def test_is_member_site_info(self): user = self.user(['DivisionMember', ]) site = MagicMock() # Needs site.siteObj site.getId.return_value = 'example' r = user_member_of_site(user, site) self.assertTrue(r) user.getRolesInContext.assert_called_once_with(site.siteObj)
def test_non_member(self): user = self.user([]) site = self.site() r = user_member_of_site(user, site) self.assertFalse(r)
def test_is_member(self): user = self.user(['DivisionMember', ]) site = self.site() r = user_member_of_site(user, site) self.assertTrue(r)