Esempio n. 1
0
        /// <summary>
        /// 授予角色
        /// </summary>
        /// <param name="role"></param>
        public Role GiveRole(Role role)
        {
            if (!Role.GetRoleList().Exists(r => r.Id == role.Id))
            {
                throw new Exception("授予的角色不存在");
            }
            if (RoleOrganizationInfoRepo.Read().Exists(ro => ro.RoleId == role.Id && ro.OrganizationId == this.Id))
            {
                return(role);
            }

            //检查互斥
            List <Role> linkRoleList = this.LinkRoleList;

            foreach (Role linkRole in linkRoleList)
            {
                if (RoleMutex.CheckMutex(role, linkRole, RoleMutexType.Static))
                {
                    throw new Exception("授予的角色和当前组织或其父级或其子级的角色存在静态互斥关系");
                }
            }

            RoleOrganizationInfo roleOrganizationInfo = new RoleOrganizationInfo();

            roleOrganizationInfo.RoleId         = role.Id;
            roleOrganizationInfo.OrganizationId = this.Id;
            RoleOrganizationInfoRepo.Save(roleOrganizationInfo);
            return(role);
        }
Esempio n. 2
0
        /// <summary>
        /// 授予角色
        /// </summary>
        /// <param name="role"></param>
        public Role GiveRole(Role role)
        {
            //角色是否已存在
            if (RoleUserInfoRepo.Read().Exists(ru => ru.RoleId == role.Id && ru.UserId == this.Id))
            {
                return(role);
            }

            //检查互斥
            List <Role> linkRoleList = this.DeepRoleList;

            foreach (Role linkRole in linkRoleList)
            {
                if (RoleMutex.CheckMutex(role, linkRole, RoleMutexType.Static))
                {
                    throw new Exception("授予的角色和当前用户或其所在的组织的角色存在静态互斥关系");
                }
            }

            RoleUserInfo roleUserInfo = new RoleUserInfo();

            roleUserInfo.RoleId = role.Id;
            roleUserInfo.UserId = this.Id;
            RoleUserInfoRepo.Add(roleUserInfo);
            return(role);
        }
Esempio n. 3
0
        public void GetRoleMutexListBygroup()
        {
            RoleMutex rolemutex = new RoleMutex();
            rolemutex.Group = "test-add-rolemutex";
            rolemutex.Save();

            List<RoleMutex> rolemutex1 = RoleMutex.GetRoleMutexList(rolemutex.Group);
            Assert.IsNotNull(rolemutex1);

            rolemutex.Delete();
        }
Esempio n. 4
0
        public void Delete()
        {
            RoleMutex rolemutex = new RoleMutex();
            rolemutex.Group = StringFactory.NewGuid();
            rolemutex.Type = RoleMutexType.Static;
            rolemutex.RoleId = StringFactory.NewGuid();
            rolemutex.Save();

            List<RoleMutex> list = RoleMutex.GetRoleMutexList();
            Assert.IsNotNull(list.Where(r => r.Group == rolemutex.Group).ToList());

            rolemutex.Delete();

            var rolemutex1 = RoleMutex.GetRoleMutexList().FirstOrDefault(r => r.Group == rolemutex.Group);
            Assert.IsNull(rolemutex1);
        }
Esempio n. 5
0
        public void CheckMutex()
        {
            RoleMutex rolemutex = new RoleMutex();
            rolemutex.Group = "test-add-rolemutex";
            rolemutex.Save();

            Role role1 = Role.Create();
            role1.Name = "test-check-rolemutex";
            role1.Save();

            Role role2 = Role.Create();
            role2.Name = "test-check-rolemutex2";
            role2.Save();

            bool b = RoleMutex.CheckMutex(role1, role2, RoleMutexType.Static);
            Assert.IsFalse(b);

            rolemutex.Delete();
            role1.Delete();
            role2.Delete();
        }
Esempio n. 6
0
 /// <summary>
 /// 新增子节点
 /// </summary>
 /// <param name="organization"></param>
 public Organization AddChild(Organization organization)
 {
     //设置上级id
     if (organization.ParentId != this.Id)
     {
         //检查互斥
         List <Role> roleList1 = this.LinkRoleList;
         List <Role> roleList2 = organization.LinkRoleList;
         foreach (Role role1 in roleList1)
         {
             foreach (Role role2 in roleList1)
             {
                 if (RoleMutex.CheckMutex(role1, role2, RoleMutexType.Static))
                 {
                     throw new Exception("当前组织或其父级或其子级的角色存在静态互斥关系");
                 }
             }
         }
         organization.ParentId = this.Id;
         organization.Save();
     }
     return(organization);
 }
Esempio n. 7
0
        public Role GiveRole(Role role)
        {
            if (!PositionInfoRepo.Exists(this.MappingTo <PositionInfo>()))
            {
                throw new Exception("岗位不存在");
            }
            if (!Role.RoleInfoRepo.Exists(role.MappingTo <RoleInfo>()))
            {
                throw new Exception("角色不存在");
            }
            if (RolePositionInfoRepo.Read().Exists(ro => ro.RoleId == role.Id &&
                                                   ro.OrganizationId == this.OrganizationId &&
                                                   ro.PositionId == this.Id))
            {
                return(role);
            }

            //检查互斥,除检查自身外,需检查所有上级组件层叠过来的角色(DeepRoleList 包括上级组件的角色)
            List <Role> linkRoleList = this.DeepRoleList;

            foreach (Role linkRole in linkRoleList)
            {
                if (RoleMutex.CheckMutex(role, linkRole, RoleMutexType.Static))
                {
                    throw new Exception("授予的角色和当前岗位或其所在组织的角色存在静态互斥关系");
                }
            }

            RolePositionInfo rolePositionInfo = new RolePositionInfo();

            rolePositionInfo.RoleId         = role.Id;
            rolePositionInfo.OrganizationId = this.OrganizationId;
            rolePositionInfo.PositionId     = this.Id;
            RolePositionInfoRepo.Add(rolePositionInfo);
            return(role);
        }
Esempio n. 8
0
        public void RemoveGroup()
        {
            RoleMutex rolemutex = new RoleMutex();
            rolemutex.Group = "test-add-rolemutex";
            rolemutex.RoleId = StringFactory.NewGuid();
            rolemutex.Save();

            List<RoleMutex> list = RoleMutex.GetRoleMutexList();
            Assert.IsTrue(list.Where(r => r.Group == rolemutex.Group).ToList().Count > 0);

            RoleMutex.RemoveGroup(rolemutex.Group);

            List<RoleMutex> newlist = RoleMutex.GetRoleMutexList();
            Assert.AreEqual(0, newlist.Where(r => r.Group == rolemutex.Group).ToList().Count);
            rolemutex.Delete();
        }
Esempio n. 9
0
        public void Save()
        {
            RoleMutex rolemutex = new RoleMutex();
            rolemutex.Group = StringFactory.NewGuid();
            rolemutex.Type = RoleMutexType.Static;
            List<RoleMutex> list1 = RoleMutex.GetRoleMutexList();
            Assert.AreEqual(0, list1.Where(r => r.Group == rolemutex.Group).ToList().Count);
            rolemutex.Save();

            List<RoleMutex> list = RoleMutex.GetRoleMutexList();
            Assert.IsNotNull(list.Where(r => r.Group == rolemutex.Group).ToList());

            rolemutex.Delete();
        }