Beispiel #1
0
 /// <summary>
 /// adminUser.Id必须赋值
 /// </summary>
 public List<AdminUserRole> GetRolesOfAdminUser(AdminUser adminUser)
 {
     var aurs = from aur in dbContext.AdminUserRoles
                where aur.AdminUserId == adminUser.Id
                select aur;
     return aurs.ToList();
 }
Beispiel #2
0
        /// <summary>
        /// 更新管理员, 返回受影响的行数
        /// </summary>
        public int UpdateAdminUser(AdminUser adminUser)
        {
            CheckAdminUserNotNull(adminUser);

            AdminUser au = dbContext.AdminUsers.Where(u => u.Id == adminUser.Id).FirstOrDefault();
            if (au == null)
            {
                throw new Exception(string.Format("没有找到指定Id:{0}的管理员账号", adminUser.Id));
            }
            foreach (var field in typeof(AdminUser).GetProperties())
            {
                field.SetValue(au, field.GetValue(adminUser));
            }
            return dbContext.SaveChanges();
        }
Beispiel #3
0
        /// <summary>
        /// 添加管理员, 并分配权限, 返回受影响的行数
        /// </summary>
        public int AddAdminUser(AdminUser adminUser, List<long> roleIds)
        {
            CheckAdminUserNotNull(adminUser);
            if (adminUser.Id != default(long))
            {
                throw new Exception("不能指定Id的值");
            }

            dbContext.AdminUsers.Add(adminUser);
            dbContext.Entry(adminUser).State = EntityState.Added;       // 添加管理员
            foreach (long roleId in roleIds)                            // 分配角色
            {
                AdminUserRole aur = new AdminUserRole()
                {
                    // Id
                    AdminUserId = adminUser.Id,
                    RoleId = roleId,
                };
                dbContext.AdminUserRoles.Add(aur);
                dbContext.Entry(aur).State = EntityState.Added;
            }
            return dbContext.SaveChanges();
        }
Beispiel #4
0
        /// <summary>
        /// 检查管理员是否能登录
        /// </summary>
        public AdminUserLoginStatus DoLogin(string userName, string password, out AdminUser adminUser)
        {
            if (CommonHelper.IsNullOrEmptyOrWhiteSpace(userName, password))
            {
                adminUser = null;
                return AdminUserLoginStatus.CannotNull;
            }

            adminUser = dal.GetAdminUser(userName);
            if (adminUser == null)
            {
                return AdminUserLoginStatus.UserNameNotFound;
            }
            if (!adminUser.IsEnabled)
            {
                return AdminUserLoginStatus.Disabled;
            }
            if (adminUser.Password != CommonHelper.CalcMd5(password))
            {
                return AdminUserLoginStatus.PasswordError;
            }
            return AdminUserLoginStatus.Success;
        }
Beispiel #5
0
 private void CheckAdminUserNotNull(AdminUser adminUser)
 {
     if (adminUser == null)
     {
         throw new ArgumentNullException("参数adminUser不能为null");
     }
 }
Beispiel #6
0
        public int DeleteAdminUser(AdminUser adminUser)
        {
            CheckAdminUserNotNull(adminUser);

            dbContext.Entry(adminUser).State = EntityState.Deleted;
            return dbContext.SaveChanges();
        }
Beispiel #7
0
        /// <summary>
        /// 得到指定Id的管理员. 顺序匹配Id,UserName
        /// </summary>
        public AdminUser GetAdminUser(AdminUser adminUser)
        {
            CheckAdminUserNotNull(adminUser);

            var admin = from AdminUser in this.dbContext.AdminUsers
                        where AdminUser.Id == adminUser.Id ||
                        AdminUser.UserName == adminUser.UserName
                        select AdminUser;
            return admin.FirstOrDefault();
        }
Beispiel #8
0
        public AdminUserAddNewStatus AddAdminUser(string username, string password, List<long> roleIds)
        {
            if (CommonHelper.IsNullOrEmptyOrWhiteSpace(username, password) || roleIds.Count <= 0)
            {
                throw new ArgumentNullException("username和password不能为null, string.Empty或只包含空格. roleIds至少包含一个元素");
            }
            int dbNums = new RoleBLL(new HengNuoWangDBContext()).GetRoles(roleIds).Count;
            if (dbNums != roleIds.Count)
            {
                throw new Exception(string.Format("‘{0}’中存在没有的角色", string.Join(",", roleIds)));
            }

            AdminUser au = GetAdminUser(username);
            if (au != null)
            {
                return AdminUserAddNewStatus.UserNameExist;
            }
            au = new AdminUser()
            {
                // Id
                UserName = username,
                Password = CommonHelper.CalcMd5(password),
                IsEnabled = true,
            };
            int row = dal.AddAdminUser(au, roleIds);
            if (row != (roleIds.Count + 1))
            {
                throw new ArgumentNullException(string.Format("出大事了,{0}添加了多条数据:{1}", au.UserName, string.Join(",", roleIds)));
            }
            return AdminUserAddNewStatus.Success;
        }
Beispiel #9
0
        /// <summary>
        /// 更新管理员, 返回受影响的行数
        /// </summary>
        public int UpdateAdminUser(AdminUser adminUser)
        {
            CheckAdminUserNotNull(adminUser);

            return dal.UpdateAdminUser(adminUser);
        }
Beispiel #10
0
 public List<Role> GetRolesOfAdminUser(AdminUser adminUser)
 {
     var join = from AdminUserRole in dbContext.AdminUserRoles
                join Role in dbContext.Roles on AdminUserRole.RoleId equals Role.Id into roles
                where AdminUserRole.AdminUserId == adminUser.Id
                from role in roles.DefaultIfEmpty()
                select role;
     return join.ToList();
 }