Open Source Web Application Framework for ASP.NET Core
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.
 
 
 
 
 
 

96 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; }
protected IIdentityUserAppService IdentityUserAppService { get; }
protected IIdentityRoleAppService IdentityRoleAppService { get; }
public CreateModalModel(IIdentityUserAppService identityUserAppService, IIdentityRoleAppService identityRoleAppService)
{
IdentityUserAppService = identityUserAppService;
IdentityRoleAppService = identityRoleAppService;
}
public virtual 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 virtual 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; }
}
}
}