Browse Source

Identity Role Properties

pull/574/head
Yunus Emre Kalkan 8 years ago
parent
commit
a2a2c1794e
  1. 4
      modules/identity/src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IdentityRoleCreateOrUpdateDtoBase.cs
  2. 6
      modules/identity/src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IdentityRoleDto.cs
  3. 5
      modules/identity/src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityRoleAppService.cs
  4. 2
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityDataSeeder.cs
  5. 20
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityRole.cs
  6. 31
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityRoleManager.cs
  7. 3
      modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityDbContextModelBuilderExtensions.cs
  8. 3
      modules/identity/src/Volo.Abp.Identity.Web/Localization/Resources/AbpIdentity/en.json
  9. 3
      modules/identity/src/Volo.Abp.Identity.Web/Localization/Resources/AbpIdentity/tr.json
  10. 8
      modules/identity/src/Volo.Abp.Identity.Web/Pages/Identity/Roles/CreateModal.cshtml
  11. 6
      modules/identity/src/Volo.Abp.Identity.Web/Pages/Identity/Roles/CreateModal.cshtml.cs
  12. 9
      modules/identity/src/Volo.Abp.Identity.Web/Pages/Identity/Roles/EditModal.cshtml
  13. 8
      modules/identity/src/Volo.Abp.Identity.Web/Pages/Identity/Roles/EditModal.cshtml.cs
  14. 4
      modules/identity/src/Volo.Abp.Identity.Web/Pages/Identity/Roles/index.js
  15. 11
      modules/identity/src/Volo.Abp.Identity.Web/Pages/Identity/Users/CreateModal.cshtml.cs

4
modules/identity/src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IdentityRoleCreateOrUpdateDtoBase.cs

@ -7,5 +7,9 @@ namespace Volo.Abp.Identity
[Required]
[StringLength(IdentityRoleConsts.MaxNameLength)]
public string Name { get; set; }
public bool IsDefault { get; set; }
public bool IsPublic { get; set; }
}
}

6
modules/identity/src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IdentityRoleDto.cs

@ -6,5 +6,11 @@ namespace Volo.Abp.Identity
public class IdentityRoleDto : EntityDto<Guid>
{
public string Name { get; set; }
public bool IsDefault { get; set; }
public bool IsStatic { get; set; }
public bool IsPublic { get; set; }
}
}

5
modules/identity/src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityRoleAppService.cs

@ -69,7 +69,7 @@ namespace Volo.Abp.Identity
[Authorize(IdentityPermissions.Roles.Create)]
public async Task<IdentityRoleDto> CreateAsync(IdentityRoleCreateDto input)
{
var role = new IdentityRole(GuidGenerator.Create(), input.Name, CurrentTenant.Id);
var role = new IdentityRole(GuidGenerator.Create(), input.Name, input.IsDefault, false, input.IsPublic, CurrentTenant.Id);
(await _roleManager.CreateAsync(role)).CheckErrors();
await CurrentUnitOfWork.SaveChangesAsync();
@ -84,6 +84,9 @@ namespace Volo.Abp.Identity
(await _roleManager.SetRoleNameAsync(role, input.Name)).CheckErrors();
role.IsDefault = input.IsDefault;
role.IsPublic = input.IsPublic;
(await _roleManager.UpdateAsync(role)).CheckErrors();
await CurrentUnitOfWork.SaveChangesAsync();

2
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityDataSeeder.cs

@ -62,7 +62,7 @@ namespace Volo.Abp.Identity
var adminRole = await _roleRepository.FindByNormalizedNameAsync(_lookupNormalizer.Normalize(adminRoleName));
if (adminRole == null)
{
adminRole = new IdentityRole(_guidGenerator.Create(), adminRoleName, tenantId);
adminRole = new IdentityRole(_guidGenerator.Create(), adminRoleName, false, true, true, tenantId);
CheckIdentityErrors(await _roleManager.CreateAsync(adminRole));
if (adminRolePermissions != null)

20
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityRole.cs

@ -39,12 +39,27 @@ namespace Volo.Abp.Identity
[DisableAuditing]
public virtual string ConcurrencyStamp { get; set; }
/// <summary>
/// A default role is automatically assigned to a new user
/// </summary>
public virtual bool IsDefault { get; set; }
/// <summary>
/// A static role can not be deleted/renamed
/// </summary>
public virtual bool IsStatic { get; protected set; }
/// <summary>
/// A user can see other user's public roles
/// </summary>
public virtual bool IsPublic { get; set; }
/// <summary>
/// Initializes a new instance of <see cref="IdentityRole"/>.
/// </summary>
protected IdentityRole() { }
public IdentityRole(Guid id, [NotNull] string name, Guid? tenantId = null)
public IdentityRole(Guid id, [NotNull] string name, bool isDefault = false, bool isStatic = false, bool isPublic = false, Guid? tenantId = null)
{
Check.NotNull(name, nameof(name));
@ -53,6 +68,9 @@ namespace Volo.Abp.Identity
TenantId = tenantId;
NormalizedName = name.ToUpperInvariant();
ConcurrencyStamp = Guid.NewGuid().ToString();
IsDefault = isDefault;
IsStatic = isStatic;
IsPublic = isPublic;
Claims = new Collection<IdentityRoleClaim>();
}

31
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityRoleManager.cs

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
@ -43,5 +44,35 @@ namespace Volo.Abp.Identity
return role;
}
public List<IdentityRole> GetDefaultRoles()
{
if (Roles == null)
{
return new List<IdentityRole>();
}
return Roles.Where(r => r.IsDefault).ToList();
}
public override async Task<IdentityResult> SetRoleNameAsync(IdentityRole role, string name)
{
if (role.IsStatic && role.Name != name)
{
throw new AbpException("Static roles can not be renamed."); // TODO: localize & change exception type
}
return await base.SetRoleNameAsync(role,name);
}
public override async Task<IdentityResult> DeleteAsync(IdentityRole role)
{
if (role.IsStatic)
{
throw new AbpException("Static roles can not be deleted."); // TODO: localize & change exception type
}
return await base.DeleteAsync(role);
}
}
}

3
modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityDbContextModelBuilderExtensions.cs

@ -97,6 +97,9 @@ namespace Volo.Abp.Identity.EntityFrameworkCore
b.Property(r => r.Name).IsRequired().HasMaxLength(IdentityRoleConsts.MaxNameLength);
b.Property(r => r.NormalizedName).IsRequired().HasMaxLength(IdentityRoleConsts.MaxNormalizedNameLength);
b.Property(r => r.IsDefault).HasColumnName(nameof(IdentityRole.IsDefault));
b.Property(r => r.IsStatic).HasColumnName(nameof(IdentityRole.IsStatic));
b.Property(r => r.IsPublic).HasColumnName(nameof(IdentityRole.IsPublic));
b.HasMany(r => r.Claims).WithOne().HasForeignKey(rc => rc.RoleId).IsRequired();

3
modules/identity/src/Volo.Abp.Identity.Web/Localization/Resources/AbpIdentity/en.json

@ -8,6 +8,9 @@
"EmailAddress": "Email address",
"PhoneNumber": "Phone number",
"UserInformations": "User informations",
"DisplayName:IsDefault": "Default",
"DisplayName:IsStatic": "Static",
"DisplayName:IsPublic": "Public",
"Roles": "Roles",
"Password": "Password",
"UserDeletionConfirmationMessage": "User '{0}' will be deleted. Do you confirm that?",

3
modules/identity/src/Volo.Abp.Identity.Web/Localization/Resources/AbpIdentity/tr.json

@ -6,6 +6,9 @@
"NewUser": "Yeni kullanıcı",
"UserName": "Kullanıcı adı",
"EmailAddress": "E-posta adresi",
"DisplayName:IsDefault": "Varsayılan",
"DisplayName:IsStatic": "Sabit",
"DisplayName:IsPublic": "Herkese Açık",
"PhoneNumber": "Telefon numarası",
"UserInformations": "Kullanıcı bilgileri",
"Roles": "Roller",

8
modules/identity/src/Volo.Abp.Identity.Web/Pages/Identity/Roles/CreateModal.cshtml

@ -7,12 +7,14 @@
@{
Layout = null;
}
<abp-dynamic-form abp-model="@Model.Role" asp-page="/Identity/Roles/CreateModal">
<form asp-page="/Identity/Roles/CreateModal">
<abp-modal>
<abp-modal-header title="@L["NewRole"].Value"></abp-modal-header>
<abp-modal-body>
<abp-form-content />
<abp-input asp-for="Role.Name" />
<abp-input asp-for="Role.IsDefault" />
<abp-input asp-for="Role.IsPublic" />
</abp-modal-body>
<abp-modal-footer buttons="@(AbpModalButtons.Cancel|AbpModalButtons.Save)"></abp-modal-footer>
</abp-modal>
</abp-dynamic-form>
</form>

6
modules/identity/src/Volo.Abp.Identity.Web/Pages/Identity/Roles/CreateModal.cshtml.cs

@ -33,6 +33,12 @@ namespace Volo.Abp.Identity.Web.Pages.Identity.Roles
[StringLength(IdentityRoleConsts.MaxNameLength)]
[Display(Name = "DisplayName:RoleName")]
public string Name { get; set; }
[Display(Name = "DisplayName:IsDefault")]
public bool IsDefault { get; set; }
[Display(Name = "DisplayName:IsPublic")]
public bool IsPublic { get; set; }
}
}
}

9
modules/identity/src/Volo.Abp.Identity.Web/Pages/Identity/Roles/EditModal.cshtml

@ -7,12 +7,15 @@
@{
Layout = null;
}
<abp-dynamic-form abp-model="@Model.Role" asp-page="/Identity/Roles/EditModal">
<form asp-page="/Identity/Roles/EditModal">
<abp-modal>
<abp-modal-header title="@L["Edit"].Value"></abp-modal-header>
<abp-modal-body>
<abp-form-content/>
<abp-input asp-for="Role.Id" />
<abp-input asp-for="Role.Name" readonly="@(Model.Role.IsStatic?" readonly":"")" />
<abp-input asp-for="Role.IsDefault" />
<abp-input asp-for="Role.IsPublic" />
</abp-modal-body>
<abp-modal-footer buttons="@(AbpModalButtons.Cancel|AbpModalButtons.Save)"></abp-modal-footer>
</abp-modal>
</abp-dynamic-form>
</form>

8
modules/identity/src/Volo.Abp.Identity.Web/Pages/Identity/Roles/EditModal.cshtml.cs

@ -44,6 +44,14 @@ namespace Volo.Abp.Identity.Web.Pages.Identity.Roles
[StringLength(IdentityRoleConsts.MaxNameLength)]
[Display(Name = "DisplayName:RoleName")]
public string Name { get; set; }
[Display(Name = "DisplayName:IsDefault")]
public bool IsDefault { get; set; }
public bool IsStatic { get; set; }
[Display(Name = "DisplayName:IsPublic")]
public bool IsPublic { get; set; }
}
}
}

4
modules/identity/src/Volo.Abp.Identity.Web/Pages/Identity/Roles/index.js

@ -45,8 +45,8 @@
},
{
text: l('Delete'),
visible: function () {
return true; //TODO: Check permission
visible: function (data) {
return !data.isStatic; //TODO: Check permission
},
confirmMessage: function (data) { return l('RoleDeletionConfirmationMessage', data.record.name)},
action: function (data) {

11
modules/identity/src/Volo.Abp.Identity.Web/Pages/Identity/Users/CreateModal.cshtml.cs

@ -28,9 +28,20 @@ namespace Volo.Abp.Identity.Web.Pages.Identity.Users
{
UserInfo = new UserInfoViewModel();
var roleDtoList = await _identityRoleAppService.GetAllListAsync();
Roles = ObjectMapper.Map<List<IdentityRoleDto>, AssignedRoleViewModel[]>(
await _identityRoleAppService.GetAllListAsync()
);
var defaultRoles = roleDtoList.Where(r => r.IsDefault).ToList();
foreach (var role in Roles)
{
if (defaultRoles.Any(r => r.Name == role.Name))
{
role.IsAssigned = true;
}
}
}
public async Task<NoContentResult> OnPostAsync()

Loading…
Cancel
Save