You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
abp/modules/identity/src/Volo.Abp.Identity.Web/Pages/Identity/Users/CreateModal.cshtml.cs

97 lines
3.0 KiB

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Auditing;
using Volo.Abp.Application.Dtos;
namespace Volo.Abp.Identity.Web.Pages.Identity.Users
{
public class CreateModalModel : IdentityPageModel
{
[BindProperty]
public UserInfoViewModel UserInfo { get; set; }
[BindProperty]
public AssignedRoleViewModel[] Roles { get; set; }
private readonly IIdentityUserAppService _identityUserAppService;
private readonly IIdentityRoleAppService _identityRoleAppService;
public CreateModalModel(IIdentityUserAppService identityUserAppService, IIdentityRoleAppService identityRoleAppService)
{
_identityUserAppService = identityUserAppService;
_identityRoleAppService = identityRoleAppService;
}
public async Task OnGetAsync()
{
UserInfo = new UserInfoViewModel();
var roleDtoList = (await _identityRoleAppService.GetAllListAsync()).Items;
Roles = ObjectMapper.Map<IReadOnlyList<IdentityRoleDto>, AssignedRoleViewModel[]>(roleDtoList);
foreach (var role in Roles)
{
role.IsAssigned = role.IsDefault;
}
}
public async Task<NoContentResult> OnPostAsync()
{
ValidateModel();
var input = ObjectMapper.Map<UserInfoViewModel, IdentityUserCreateDto>(UserInfo);
input.RoleNames = Roles.Where(r => r.IsAssigned).Select(r => r.Name).ToArray();
await _identityUserAppService.CreateAsync(input);
return NoContent();
}
public class UserInfoViewModel
{
[Required]
[StringLength(IdentityUserConsts.MaxUserNameLength)]
public string UserName { get; set; }
[StringLength(IdentityUserConsts.MaxNameLength)]
public string Name { get; set; }
[StringLength(IdentityUserConsts.MaxSurnameLength)]
public string Surname { get; set; }
[Required]
[StringLength(IdentityUserConsts.MaxPasswordLength)]
[DataType(DataType.Password)]
[DisableAuditing]
public string Password { get; set; }
[Required]
[EmailAddress]
[StringLength(IdentityUserConsts.MaxEmailLength)]
public string Email { get; set; }
[StringLength(IdentityUserConsts.MaxPhoneNumberLength)]
public string PhoneNumber { get; set; }
public bool TwoFactorEnabled { get; set; } = true;
public bool LockoutEnabled { get; set; } = true;
}
public class AssignedRoleViewModel
{
[Required]
[HiddenInput]
public string Name { get; set; }
public bool IsAssigned { get; set; }
public bool IsDefault { get; set; }
}
}
}