Ejemplo n.º 1
0
        public void RegisterUser(SystemUser user, string[] roleIds)
        {
            if (roleIds.Length == 0)
            {
                throw new AuthException("必须指定角色");
            }
            using (var biz = new GameBizAuthBusinessManagement())
            {
                biz.BeginTran();
                using (var userManager = new UserManager())
                {
                    var            lastKey = userManager.GetLastUserKey();
                    IList <string> skipList;
                    user.UserId = BeautyNumberHelper.GetNextCommonNumber(lastKey, out skipList);

                    var roleList = userManager.GetRoleListByIds(roleIds);
                    if (roleList.Count != roleIds.Length)
                    {
                        throw new AuthException("指定的角色可能不存在 - " + string.Join(",", roleIds));
                    }
                    user.RoleList = roleList;
                    userManager.AddSystemUser(user);
                    userManager.UpdateLastUserKey(user.UserId, skipList.Count);
                    if (skipList.Count > 0)
                    {
                        userManager.AddSkipBeautyKey(skipList.ToArray(), lastKey, user.UserId);
                    }
                }
                biz.CommitTran();
            }
        }
Ejemplo n.º 2
0
        public void DeleteSystemRole(string roleId)
        {
            using (var biz = new GameBizAuthBusinessManagement())
            {
                biz.BeginTran();

                var roleManager = new RoleManager();
                var userManager = new UserManager();

                var role = roleManager.GetRoleById(roleId);
                if (role == null)
                {
                    throw new ArgumentException("指定编号的角色不存在 - " + roleId);
                }
                if (role.IsInner)
                {
                    throw new ArgumentException("不允许删除内置角色 - " + role.RoleName);
                }
                var childrenCount = userManager.GetRoleChildrenCount(roleId);
                if (childrenCount > 0)
                {
                    throw new ArgumentException(string.Format("指定删除的角色\"{0}\"拥有子节点,不允许被删除", role.RoleName));
                }
                var userCount = userManager.GetRoleContainUserCount(roleId);
                if (userCount > 0)
                {
                    throw new ArgumentException(string.Format("指定删除的角色\"{0}\"被分配给 {1}个用户,不允许删除", role.RoleName, userCount));
                }
                roleManager.DeleteRole(role);

                biz.CommitTran();
            }
        }
Ejemplo n.º 3
0
 public void RemoveUserRoles(string userId, string[] roleIds)
 {
     using (var biz = new GameBizAuthBusinessManagement())
     {
         biz.BeginTran();
         using (var userManager = new UserManager())
         {
             var user = userManager.GetUserById(userId);
             if (user == null)
             {
                 throw new ArgumentException("指定的用户不存在。");
             }
             NHibernate.NHibernateUtil.Initialize(user.RoleList);
             foreach (var id in roleIds)
             {
                 foreach (var role in user.RoleList)
                 {
                     if (role.RoleId == id)
                     {
                         user.RoleList.Remove(role);
                         break;
                     }
                 }
             }
             if (user.RoleList.Count == 0)
             {
                 throw new AuthException("用户必须指定至少一个角色");
             }
             userManager.UpdateSystemUser(user);
         }
         biz.CommitTran();
     }
 }
Ejemplo n.º 4
0
        public void UpdateSystemRole(RoleInfo_Update roleInfo)
        {
            using (var biz = new GameBizAuthBusinessManagement())
            {
                biz.BeginTran();
                using (var roleManager = new RoleManager())
                {
                    var role = roleManager.GetRoleById(roleInfo.RoleId);
                    if (role == null)
                    {
                        throw new ArgumentException("指定编号的角色不存在 - " + roleInfo.RoleId);
                    }
                    role.RoleId   = roleInfo.RoleId;
                    role.RoleName = roleInfo.RoleName;
                    roleManager.UpdateRole(role);

                    foreach (var item in roleInfo.AddFunctionList)
                    {
                        var roleFunction = roleManager.GetRoleFunction(role, item.FunctionId);
                        if (roleFunction != null)
                        {
                            throw new ArgumentException("添加权限到角色错误 - 已经包含权限\"" + roleFunction.Function.FunctionId + " - " + roleFunction.Function.DisplayName + "\"");
                        }
                        roleFunction = new RoleFunction
                        {
                            Role       = role,
                            FunctionId = item.FunctionId,
                            Function   = roleManager.LoadFunctionById(item.FunctionId),
                            Status     = EnableStatus.Enable,
                            Mode       = item.Mode,
                        };
                        roleManager.AddRoleFunction(roleFunction);
                    }
                    foreach (var item in roleInfo.ModifyFunctionList)
                    {
                        var roleFunction = roleManager.GetRoleFunction(role, item.FunctionId);
                        if (roleFunction == null)
                        {
                            throw new ArgumentException("修改权限错误 - 此角色尚未包含权限\"" + roleFunction.Function.FunctionId + " - " + roleFunction.Function.DisplayName + "\"");
                        }
                        roleFunction.Mode = item.Mode;
                        roleManager.UpdateRoleFunction(roleFunction);
                    }
                    foreach (var item in roleInfo.RemoveFunctionList)
                    {
                        var roleFunction = roleManager.GetRoleFunction(role, item.FunctionId);
                        if (roleFunction == null)
                        {
                            throw new ArgumentException("移除权限错误 - 此角色尚未包含权限\"" + roleFunction.Function.FunctionId + " - " + roleFunction.Function.DisplayName + "\"");
                        }
                        roleManager.DeleteRoleFunction(roleFunction);
                    }
                }
                biz.CommitTran();
            }
        }
Ejemplo n.º 5
0
 public void AddSystemRole(RoleInfo_Add roleInfo)
 {
     using (var biz = new GameBizAuthBusinessManagement())
     {
         biz.BeginTran();
         using (var roleManager = new RoleManager())
         {
             var role = roleManager.GetRoleById(roleInfo.RoleId);
             if (role != null)
             {
                 throw new ArgumentException("指定编号的角色已经存在 - " + role.RoleId);
             }
             role = new SystemRole
             {
                 RoleId       = roleInfo.RoleId,
                 RoleName     = roleInfo.RoleName,
                 IsAdmin      = roleInfo.IsAdmin,
                 IsInner      = false,
                 RoleType     = roleInfo.RoleType,
                 FunctionList = new List <RoleFunction>(),
             };
             if (!role.IsAdmin)
             {
                 foreach (var item in roleInfo.FunctionList)
                 {
                     var roleFunction = new RoleFunction
                     {
                         Role       = role,
                         FunctionId = item.FunctionId,
                         Function   = roleManager.LoadFunctionById(item.FunctionId),
                         Status     = EnableStatus.Enable,
                         Mode       = item.Mode,
                     };
                     role.FunctionList.Add(roleFunction);
                 }
                 var list = roleManager.QueryFixFunctionList(roleInfo.RoleType);
                 foreach (var item in list)
                 {
                     var roleFunction = new RoleFunction
                     {
                         Role       = role,
                         FunctionId = item.FunctionId,
                         Function   = item,
                         Status     = EnableStatus.Enable,
                         Mode       = "RW",
                     };
                     role.FunctionList.Add(roleFunction);
                 }
             }
             roleManager.AddRole(role);
         }
         biz.CommitTran();
     }
 }
Ejemplo n.º 6
0
 public void AddUserRoles(string userId, string[] roleIds)
 {
     using (var biz = new GameBizAuthBusinessManagement())
     {
         biz.BeginTran();
         using (var userManager = new UserManager())
         {
             var user = userManager.GetUserById(userId);
             if (user == null)
             {
                 throw new ArgumentException("指定的用户不存在。");
             }
             NHibernate.NHibernateUtil.Initialize(user.RoleList);
             var roleList = userManager.GetRoleListByIds(roleIds);
             foreach (var role in roleList)
             {
                 user.RoleList.Add(role);
             }
             userManager.UpdateSystemUser(user);
         }
         biz.CommitTran();
     }
 }