예제 #1
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();
        }
예제 #2
0
        public int ChangeRole(long adminUserId, List<long> roleIds)
        {
            var select = from aur in dbContext.AdminUserRoles
                         where aur.AdminUserId == adminUserId
                         select aur;
            List<AdminUserRole> aurs = select.ToList();
            foreach (var aur in aurs)
            {
                dbContext.Entry(aur).State = EntityState.Deleted;   // 角色全部删除
            }

            foreach (var roleId in roleIds)
            {
                AdminUserRole aur = new AdminUserRole()
                {
                    // Id
                    AdminUserId = adminUserId,
                    RoleId = roleId,
                };
                dbContext.AdminUserRoles.Add(aur);
                dbContext.Entry(aur).State = EntityState.Added;    // 重新分配角色
            }
            return dbContext.SaveChanges();
        }