IwbSysRole.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.ComponentModel.DataAnnotations.Schema;
  5. using Abp.Domain.Entities.Auditing;
  6. using IwbZero.Authorization.Base.Roles;
  7. using IwbZero.Authorization.Users;
  8. using Microsoft.AspNet.Identity;
  9. namespace IwbZero.Authorization.Roles
  10. {
  11. /// <summary>
  12. /// Represents a role in an application. A role is used to group permissions.
  13. /// </summary>
  14. /// <remarks>
  15. /// Application should use permissions to check if user is granted to perform an operation.
  16. /// Checking 'if a user has a role' is not possible until the role is static (<see cref="RoleBase.IsStatic"/>).
  17. /// Static roles can be used in the code and can not be deleted by users.
  18. /// Non-static (dynamic) roles can be added/removed by users and we can not know their name while coding.
  19. /// A user can have multiple roles. Thus, user will have all permissions of all assigned roles.
  20. /// </remarks>
  21. public abstract class IwbSysRole<TUser> : RoleBase, IRole<int>, IFullAudited<TUser>
  22. where TUser : IwbSysUser<TUser>
  23. {
  24. /// <summary>
  25. /// Maximum length of the <see cref="ConcurrencyStamp"/> property.
  26. /// </summary>
  27. public const int MaxConcurrencyStampLength = 128;
  28. /// <summary>
  29. /// Unique name of this role.
  30. /// </summary>
  31. [Required]
  32. [StringLength(MaxNameLength)]
  33. public virtual string NormalizedName { get; set; }
  34. /// <summary>
  35. /// Claims of this user.
  36. /// </summary>
  37. [ForeignKey("RoleId")]
  38. public virtual ICollection<RoleClaim> Claims { get; set; }
  39. /// <summary>
  40. /// A random value that must change whenever a user is persisted to the store
  41. /// </summary>
  42. [StringLength(MaxConcurrencyStampLength)]
  43. public virtual string ConcurrencyStamp { get; set; } = Guid.NewGuid().ToString();
  44. public virtual TUser DeleterUser { get; set; }
  45. public virtual TUser CreatorUser { get; set; }
  46. public virtual TUser LastModifierUser { get; set; }
  47. protected IwbSysRole()
  48. {
  49. SetNormalizedName();
  50. }
  51. /// <summary>
  52. /// Creates a new <see cref="IwbSysRole{TUser}"/> object.
  53. /// </summary>
  54. /// <param name="tenantId">TenantId or null (if this is not a tenant-level role)</param>
  55. /// <param name="displayName">Display name of the role</param>
  56. protected IwbSysRole(int? tenantId, string displayName)
  57. : base(tenantId, displayName)
  58. {
  59. SetNormalizedName();
  60. }
  61. /// <summary>
  62. /// Creates a new <see cref="IwbSysRole{TUser}"/> object.
  63. /// </summary>
  64. /// <param name="tenantId">TenantId or null (if this is not a tenant-level role)</param>
  65. /// <param name="name">Unique role name</param>
  66. /// <param name="displayName">Display name of the role</param>
  67. protected IwbSysRole(int? tenantId, string name, string displayName)
  68. : base(tenantId, name, displayName)
  69. {
  70. SetNormalizedName();
  71. }
  72. public virtual void SetNormalizedName()
  73. {
  74. NormalizedName = Name.ToUpperInvariant();
  75. }
  76. }
  77. }