public void TestInternalGroupRemovedeventsNotPublished()
        {
            int roleSomethingUnassignedCount = 0;
            int groupSomethingRemovedCount = 0;
            DomainEventPublisher.Instance.Subscribe<GroupUnassignedFromRole>(e => roleSomethingUnassignedCount++);
            DomainEventPublisher.Instance.Subscribe<GroupGroupRemoved>(e => groupSomethingRemovedCount++);
            DomainEventPublisher.Instance.Subscribe<UserUnassignedFromRole>(e => roleSomethingUnassignedCount++);
            DomainEventPublisher.Instance.Subscribe<GroupUserRemoved>(e => groupSomethingRemovedCount++);

            Tenant tenant = this.CreateTenant();
            User user = this.CreateUser(tenant);
            Role managerRole = tenant.ProvisionRole("Manager", "A manager role.", true);
            Group group = new Group(user.TenantId, "Managers", "A group of managers.");

            Mock<IGroupRepository> groupRepository = new Mock<IGroupRepository>();
            groupRepository.Setup(r => r.GroupNamed(group.TenantId, group.Name)).Returns(group);
            Mock<IUserRepository> userRepository = new Mock<IUserRepository>();
            userRepository.Setup(r => r.UserWithUserName(user.TenantId, user.UserName)).Returns(user);

            GroupMemberService groupMemberService = new GroupMemberService(userRepository.Object, groupRepository.Object);

            managerRole.AssignGroup(group, groupMemberService);
            managerRole.AssignUser(user);

            managerRole.UnassignUser(user);
            managerRole.UnassignGroup(group);

            Assert.AreEqual(2, roleSomethingUnassignedCount);
            Assert.AreEqual(0, groupSomethingRemovedCount);
        }
示例#2
0
        public Role(TenantId tenantId, string name, string description, bool supportsNesting)
        {
            this.TenantId = tenantId;
            this.Name = name;
            this.Description = description;
            this.SupportsNesting = supportsNesting;

            this._group = CreateInternalGroup();
        }
示例#3
0
        public virtual void AssignGroup(Group group, GroupMemberService groupMemberService)
        {
            AssertionConcern.True(this.SupportsNesting, "This role does not support group nestiong.");
            AssertionConcern.NotNull(group, "Group must not be null.");
            AssertionConcern.Equals(this.TenantId, group.TenantId, "Wrong tenant for this group.");

            this.Group.AddGroup(group, groupMemberService);

            DomainEventPublisher.Instance.Publish(new GroupAssignedToRole(this.TenantId, this.Name, group.Name));
        }
        public void Add(Group group)
        {
            if (this.GroupNamed(group.TenantId, group.Name) != null) {
                throw new InvalidOperationException("Group is not unique.");
            }

            foreach(GroupMember member in group.GroupMembers) {
                member.Group = group;
            }

            _session.Save(group);
        }
 public bool IsUserInNestedGroup(Group group, User user)
 {
     foreach(GroupMember member in group.GroupMembers.Where(m=>m.IsGroup)) {
         Group nestedGroup = _groupRepository.GroupNamed(member.TenantId, member.Name);
         if(nestedGroup!=null) {
             bool isInNestedGroup = nestedGroup.IsMember(user, this);
             if(isInNestedGroup) {
                 return true;
             }
         }
     }
     return false;
 }
示例#6
0
        public virtual void AddGroup(Group group, GroupMemberService groupMemberService)
        {
            AssertionConcern.NotNull(group, "Group must not be null.");
            AssertionConcern.Equals(this.TenantId, group.TenantId, "Wrong tenant for this group.");
            AssertionConcern.False(groupMemberService.IsMemberGroup(group, this.ToGroupMember()), "Group recurrsion.");

            /*if (this.GroupMembers.Add(group.ToGroupMember()) && !this.IsInternalGroup) {
                DomainEventPublisher.Instance.Publish(new GroupGroupAdded(this.TenantId, this.Name, group.Name));
            }*/
            this.GroupMembers.Add(group.ToGroupMember());
            if(!this.IsInternalGroup) {
                DomainEventPublisher.Instance.Publish(new GroupGroupAdded(this.TenantId, this.Name, group.Name));
            }
        }
        public void TestNoRecursiveGroupings()
        {
            User user = this.CreateUser(this.CreateTenant());

            Group groupA = new Group(user.TenantId, "GroupA", "A group named GroupA.");
            Group groupB = new Group(user.TenantId, "GroupB", "A group named GroupB.");
            Group groupC = new Group(user.TenantId, "GroupC", "A group named GroupC.");

            Mock<IGroupRepository> groupRepository = new Mock<IGroupRepository>();
            groupRepository.Setup(r => r.GroupNamed(groupA.TenantId, groupA.Name)).Returns(groupA);
            groupRepository.Setup(r => r.GroupNamed(groupB.TenantId, groupB.Name)).Returns(groupB);
            groupRepository.Setup(r => r.GroupNamed(groupC.TenantId, groupC.Name)).Returns(groupC);
            Mock<IUserRepository> userRepository = new Mock<IUserRepository>();

            GroupMemberService groupMemberService = new GroupMemberService(userRepository.Object, groupRepository.Object);
            groupA.AddGroup(groupB, groupMemberService);
            groupB.AddGroup(groupC, groupMemberService);

            groupC.AddGroup(groupA, groupMemberService);
        }
        public bool IsMemberGroup(Group group, GroupMember groupMember)
        {
            bool isMember = false;

            foreach(GroupMember member in group.GroupMembers.Where(m=>m.IsGroup)) {
                if(groupMember.Equals(member)) {
                    isMember = true;
                }
                else {
                    Group nestedGroup = _groupRepository.GroupNamed(member.TenantId, member.Name);
                    if(nestedGroup!=null) {
                        isMember = this.IsMemberGroup(nestedGroup, groupMember);
                    }
                }

                if(isMember) {
                    break;
                }
            }

            return isMember;
        }
 public void Remove(Group group)
 {
     _session.Delete(group);
 }
        public void TestUserIsNotMember()
        {
            User user = this.CreateUser(this.CreateTenant());

            Group groupA = new Group(user.TenantId, "GroupA", "A group named GroupA.");
            Group groupB = new Group(user.TenantId, "GroupB", "A group named GroupB.");

            Mock<IGroupRepository> groupRepository = new Mock<IGroupRepository>();
            groupRepository.Setup(r => r.GroupNamed(groupA.TenantId, groupA.Name)).Returns(groupA);
            groupRepository.Setup(r => r.GroupNamed(groupB.TenantId, groupB.Name)).Returns(groupB);
            Mock<IUserRepository> userRepository = new Mock<IUserRepository>();
            userRepository.Setup(r => r.UserWithUserName(user.TenantId, user.UserName)).Returns(user);

            GroupMemberService groupMemberService = new GroupMemberService(userRepository.Object, groupRepository.Object);

            Assert.IsFalse(groupB.IsMember(user, groupMemberService));
            Assert.IsFalse(groupA.IsMember(user, groupMemberService));
        }
        public void TestUserIsInRole()
        {
            Tenant tenant = this.CreateTenant();
            User user = this.CreateUser(tenant);
            Role managerRole = tenant.ProvisionRole("Manager", "A manager role.", true);
            Group group = new Group(user.TenantId, "Managers", "A group of managers.");

            Mock<IGroupRepository> groupRepository = new Mock<IGroupRepository>();
            groupRepository.Setup(r => r.GroupNamed(group.TenantId, group.Name)).Returns(group);
            Mock<IUserRepository> userRepository = new Mock<IUserRepository>();
            userRepository.Setup(r => r.UserWithUserName(user.TenantId, user.UserName)).Returns(user);

            GroupMemberService groupMemberService = new GroupMemberService(userRepository.Object, groupRepository.Object);

            managerRole.AssignGroup(group, groupMemberService);
            group.AddUser(user);

            Assert.IsTrue(group.IsMember(user, groupMemberService));
            Assert.IsTrue(managerRole.IsInRole(user, groupMemberService));
        }
示例#12
0
        public virtual void UnassignGroup(Group group)
        {
            AssertionConcern.True(this.SupportsNesting, "This role does not support group nestiong.");
            AssertionConcern.NotNull(group, "Group must not be null.");
            AssertionConcern.Equals(this.TenantId, group.TenantId, "Wrong tenant for this group.");

            this.Group.RemoveGroup(group);

            DomainEventPublisher.Instance.Publish(new GroupUnassignedFromRole(this.TenantId, this.Name, group.Name));
        }
 public bool ConfirmUser(Group group, User user)
 {
     User confirmedUser = _userRepository.UserWithUserName(group.TenantId, user.UserName);
     return confirmedUser != null && confirmedUser.IsEnabled;
 }
示例#14
0
        public virtual void RemoveGroup(Group group)
        {
            AssertionConcern.NotNull(group, "Group must not be null.");
            AssertionConcern.Equals(this.TenantId, group.TenantId, "Wrong tenant for this group.");

            if(this.GroupMembers.Remove(group.ToGroupMember()) && !this.IsInternalGroup) {
                DomainEventPublisher.Instance.Publish(new GroupGroupRemoved(this.TenantId, this.Name, group.Name));
            }
        }