Exemplo n.º 1
0
        public async Task CreateUser(CreateUserDto input)
        {
            var user = input.MapTo<UserEntity>();
            user.Id = Guid.NewGuid();

            if (!input.Password.IsNullOrEmpty())
            {
                var rs = await new PasswordValidator().ValidateAsync(input.Password);
                rs.CheckErrors();
            }
            else
            {
                input.Password = UserEntity.CreateRandomPassword();
            }

            user.Password = new PasswordHasher().HashPassword(input.Password);


            if (await _repository.FirstOrDefaultAsync(x => x.UserName == user.UserName) != null)
            {
                throw new UserFriendlyException(string.Format(L("Identity.DuplicateName"), user.UserName));
            }

            if (await _repository.FirstOrDefaultAsync(x => x.Email == user.Email) != null)
            {
                throw new UserFriendlyException(string.Format(L("Identity.DuplicateEmail"), user.Email));
            }

            if (AbpSession.TenantId.HasValue && AbpSession.TenantId != default(Guid))
            {
                user.TenantId = AbpSession.TenantId.Value;
            }

            await _repository.InsertAsync(user);
        }
Exemplo n.º 2
0
        public async Task<ActionResult> Create(CreateUserDto model, FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    using (var uow = _unitOfWorkManager.Begin())
                    {
                        model.IsActive = true;
                        var userId = await _userAppService.CreateUser(model);

                        var roles = await _roleAppService.GetRoles(new GetRolesInput());
                        var list = new List<UserRoleDto>();
                        foreach (var item in roles.Items)
                        {
                            var info = new UserRoleDto();
                            var chkName = "Role_" + item.Id;
                            var chkVal = collection[chkName];
                            if (chkVal == "on")
                            {
                                info.RoleId = item.Id;
                                info.UserId = userId;
                                info.Status = true;
                                list.Add(info);
                            }
                        }

                        await _userAppService.CreateOrUpdate(list);

                        await uow.CompleteAsync();
                    }

                    var lang = string.Format(L("Created.RecordSucceed").Localize(), model.UserName);

                    this.AddModelMessage("", lang, MessageTypes.Information);
                }
                catch (Exception ex)
                {
                    this.AddModelMessage("exception", ex.Message);
                }
            }

            return RedirectToAction("Index");
        }