Exemplo n.º 1
0
        public IActionResult Create(UserCreateViewModel model)
        {
            ResponseModel response = ResponseModelFactory.CreateInstance;

            if (model.LoginName.Trim().Length <= 0)
            {
                response.SetFailed("请输入登录名称");
                return(Ok(response));
            }
            if (_dbContext.DncUser.Count(x => x.LoginName == model.LoginName) > 0)
            {
                response.SetFailed("登录名已存在");
                return(Ok(response));
            }
            DncUser entity = _mapper.Map <UserCreateViewModel, DncUser>(model);

            entity.CreatedOn = DateTime.Now;
            entity.Guid      = Guid.NewGuid();
            entity.Status    = model.Status;
            _dbContext.DncUser.Add(entity);
            _dbContext.SaveChanges();
            response.SetSuccess();
            response.SetData(entity.Guid);
            return(Ok(response));
        }
Exemplo n.º 2
0
 public IActionResult Edit(Guid guid)
 {
     using (_dbContext)
     {
         DncUser       entity   = _dbContext.DncUser.FirstOrDefault(x => x.Guid == guid);
         ResponseModel response = ResponseModelFactory.CreateInstance;
         response.SetData(_mapper.Map <DncUser, UserEditViewModel>(entity));
         return(Ok(response));
     }
 }
Exemplo n.º 3
0
        public async Task <IActionResult> Alert()
        {
            // 请求响应实体工厂类

            // 定义JwtClaimModel,初次登录
            JwtClaimModel jcm = new JwtClaimModel();

            jcm.JwtGuidUser = Guid.NewGuid().ToString("N");;
            jcm.GuidUser    = jcm.JwtGuidUser;
            jcm.GuidGroup   = "";
            jcm.Type        = "";

            // 生成jwt的token数据
            dynamic jwtData = AuthHelper.Cas.Jwt.BuildJwtToken(_requirement, jcm);

            // 将用户数据写入Redis, 1分钟有效,老系统使用后销毁
            string redisKey = "SerpLogin:" + jwtData.token_md5;

            response.SetData(jwtData);

            return(Ok(response));
        }
Exemplo n.º 4
0
        public IActionResult AutoCreate([FromBody] AutoCreateUserViewModel model, bool autoPassword = true, bool returnToken = false)
        {
            ResponseModel response = ResponseModelFactory.CreateInstance;

            //查找用户是否已经存在
            DncUser target = _dbContext.DncUser.FirstOrDefault(x => (!string.IsNullOrWhiteSpace(model.WeChatUnionID) && x.WeChatUnionID == model.WeChatUnionID) || x.PhoneNumber == model.PhoneNumber);

            //不存在时自动创建
            if (target == default)
            {
                UserCreateViewModel user = new UserCreateViewModel
                {
                    LoginName   = model.PhoneNumber ?? model.WeChatUnionID,
                    DisplayName = model.PhoneNumber,
                    PhoneNumber = model.PhoneNumber,
                    IsLocked    = IsLocked.UnLocked,
                    Status      = UserStatus.Normal,
                    UserType    = UserType.GeneralUser,
                    IsDeleted   = IsDeleted.No,
                };
                if (autoPassword)
                {
                    string password = model.PhoneNumber ?? DateTime.Now.ToString();
                    user.Password = model.PhoneNumber != null ? $"{user.LoginName}${PasswordCalculator.SaltPassword(user.LoginName, PasswordCalculator.Md5(password))}" : null;
                }
                Create(user);
                target = _dbContext.DncUser.FirstOrDefault(x => (!string.IsNullOrWhiteSpace(model.WeChatUnionID) && x.WeChatUnionID.Trim() == model.WeChatUnionID) || x.PhoneNumber.Trim() == model.PhoneNumber);
            }

            //依然为default则创建失败
            if (target == default)
            {
                response.SetFailed("用户创建失败");
            }
            else
            {
                //如果Permission不为空,则检查是否具有对应的权限位
                if (!string.IsNullOrWhiteSpace(model.Permission))
                {
                    IQueryable <DncUserRoleMapping> targetPermission = from role in _dbContext.DncUserRoleMapping
                                                                       where role.UserGuid == target.Guid
                                                                       join permission in _dbContext.DncRolePermissionMapping on role.RoleCode equals permission.RoleCode
                                                                       where permission.PermissionCode == model.Permission
                                                                       select role;
                    if (targetPermission.FirstOrDefault() == default)
                    {
                        _dbContext.DncUserRoleMapping.Add(new DncUserRoleMapping
                        {
                            CreatedOn = DateTime.Now,
                            UserGuid  = target.Guid,
                            RoleCode  = model.Permission
                        });
                        _dbContext.SaveChanges();
                    }
                }

                //创建返回体
                dynamic result = new ExpandoObject();
                result.Guid = target.Guid;
                if (returnToken)
                {
                    result.Authorization = TokenBulider(target);
                }
                response.SetData(result);
            }
            return(Ok(response));


            string TokenBulider(DncUser user)
            {
                if (user == null || user.IsDeleted == IsDeleted.Yes)
                {
                    throw new InvalidOperationException("用户不存在");
                }

                if (user.IsLocked == IsLocked.Locked)
                {
                    throw new InvalidOperationException("账号已被锁定");
                }

                if (user.Status == UserStatus.Forbidden)
                {
                    throw new InvalidOperationException("账号已被禁用");
                }

                ClaimsIdentity claimsIdentity = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.Name, user.LoginName),
                    new Claim("guid", user.Guid.ToString()),
                    new Claim("avatar", ""),
                    new Claim("displayName", user.DisplayName),
                    new Claim("loginName", user.LoginName),
                    new Claim("emailAddress", ""),
                    new Claim("guid", user.Guid.ToString()),
                    new Claim("userType", ((int)user.UserType).ToString()),
                });

                return(JwtBearerAuthenticationExtension.GetJwtAccessToken(_appSettings, claimsIdentity));
            }
        }