コード例 #1
0
        public void SaveRole(Sender sender, SaveRoleArgs args)
        {
            try
            {
                if (string.IsNullOrEmpty(args.Role.RoleName))
                {
                    throw new Exception(string.Format("角色名称:{0}命名无效,可能存在特殊字符。", args.Role.RoleName));
                }

                if (RoleIsDuplicated(sender, args.Role))
                {
                    throw new Exception(string.Format("角色名称:{0}已存在,请重新输入。", args.Role.RoleName));
                }

                using (ObjectProxy op = new ObjectProxy(true))
                {
                    Role role = new Role();
                    role.RoleID = args.Role.RoleID;
                    if (op.LoadRoleByRoleID(role) == 0)
                    {
                        role = null;
                    }

                    if (role == null)
                    {
                        args.Role.Created    = DateTime.Now;
                        args.Role.CreatedBy  = sender.UserCode + "." + sender.UserName;
                        args.Role.Modified   = args.Role.Created;
                        args.Role.ModifiedBy = args.Role.CreatedBy;
                        op.InsertRole(args.Role);
                    }
                    else
                    {
                        args.Role.Modified   = DateTime.Now;
                        args.Role.ModifiedBy = sender.UserCode + "." + sender.UserName;
                        op.UpdateRoleByRoleID(args.Role);
                    }
                    if (args.UserIDs != null)
                    {
                        op.DeleteUser2RolesByRoleID(args.Role.RoleID);
                        foreach (Guid userID in args.UserIDs)
                        {
                            User2Role ug = new User2Role();
                            ug.UserID = userID;
                            ug.RoleID = args.Role.RoleID;
                            op.InsertUser2Role(ug);
                        }
                    }

                    if (args.PrivilegeItemIDs != null)
                    {
                        op.DeleteRole2PrivilegeItemsByRoleID(args.Role.RoleID);

                        foreach (Guid privilege in args.PrivilegeItemIDs)
                        {
                            Role2PrivilegeItem rp = new Role2PrivilegeItem();
                            rp.RoleID          = args.Role.RoleID;
                            rp.PrivilegeItemID = privilege;
                            op.InsertRole2PrivilegeItem(rp);
                        }
                    }
                    op.CommitTransaction();
                }
            }
            catch (Exception ex)
            {
                PLogger.LogError(ex);
                throw ex;
            }
        }