예제 #1
0
        public OperateResult <IEnumerable <Auth_RoleUser> > BindRoles(UserBindRolesParam param)
        {
            //校验用户
            if (!_db.Auth_User.Any(l => l.Id == param.UserId))
            {
                return(OperateResult <IEnumerable <Auth_RoleUser> > .Error("用户不存在"));
            }
            //删除用户的角色
            var userRoles = _db.Auth_RoleUser.Where(l => l.UserId == param.UserId);
            var repo      = _unitOfWork.GetRepository <Auth_RoleUser>();

            repo.Delete(userRoles);
            //生成实体
            var list = new List <Auth_RoleUser>();

            foreach (var roleId in param.RoleIds)
            {
                var obj = new Auth_RoleUser()
                {
                    Id     = Guid.NewGuid(),
                    UserId = param.UserId,
                    RoleId = roleId
                };
                list.Add(obj);
                repo.Insert(obj);
            }
            _unitOfWork.SaveChanges();
            return(OperateResult <IEnumerable <Auth_RoleUser> > .Ok("成功", list));
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            //添加初始数据
            modelBuilder.Entity <Auth_Menu>().HasData(new Auth_Menu()
            {
                Id       = new Guid("1e229079-e8da-4db2-ae94-3160ba229b14"),
                Url      = "",
                Name     = "默认",
                ParentId = Guid.Empty,
                Remark   = "默认菜单"
            });
            //默认管理员角色
            var role = new Auth_Role()
            {
                Id   = new Guid("b7f744b4-4f18-4a56-a3a4-a8ea9933f998"),
                Code = "admin",
                Name = "超级管理员"
            };

            modelBuilder.Entity <Auth_Role>().HasData(role);
            //默认账号--接口测试账号
            var user = new Auth_User()
            {
                Id       = new Guid("e133e990-e216-4273-b7d5-7720b0fc4c45"),
                UserName = "******",
                RealName = "接口测试账号",
                Password = "******".To32MD5()
            };
            var userWeb = new Auth_User()
            {
                Id       = new Guid("be6b10d5-e9f3-4224-b0d4-35dfe4af582a"),
                UserName = "******",
                RealName = "系统管理员",
                Password = "******".To32MD5()
            };

            modelBuilder.Entity <Auth_User>().HasData(user);
            modelBuilder.Entity <Auth_User>().HasData(userWeb);
            var userRole = new Auth_RoleUser()
            {
                Id     = new Guid("365584e2-cc61-4043-b142-1dc5683d49de"),
                RoleId = role.Id,
                UserId = user.Id
            };

            modelBuilder.Entity <Auth_RoleUser>().HasData(userRole);
        }