Exemplo n.º 1
0
 public bool HasPermissions(UserAuthorizationEntity entity) =>
 (
     from expectedUserPermission in ExpectedUserPermissions
     let userPermissions = entity.Permissions.FirstOrDefault(item => item.PermissionGroupId == expectedUserPermission.PermissionGroupId)
                               where userPermissions != null && (expectedUserPermission.Permissions == 0 || (userPermissions.Permissions & expectedUserPermission.Permissions) != 0)
                           select true
 ).Any();
Exemplo n.º 2
0
        /// <summary>
        /// 从数据库中删除给定的用户权限实体数据。
        /// </summary>
        /// <param name="entity">要删除的实体数据。</param>
        /// <exception cref="ArgumentNullException"/>
        public void RemoveUserAuthorizationEntity(UserAuthorizationEntity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            UserAuthorization.Remove(entity);
            SaveChanges();
        }
 internal static UserAuthorizationModel ToModel(this UserAuthorizationEntity entity) => new UserAuthorizationModel
 {
     Id          = entity.Id,
     Email       = entity.Email,
     FirstName   = entity.FirstName,
     LastName    = entity.LastName,
     Color       = entity.Color,
     UserRoleId  = entity.UserRoleId,
     Permissions = entity.Permissions
 };
Exemplo n.º 4
0
        /// <summary>
        /// 将给定的用户权限实体数据添加至数据库中。
        /// </summary>
        /// <param name="entity">要添加的用户权限实体数据。</param>
        /// <exception cref="ArgumentNullException"/>
        /// <exception cref="InvalidOperationException"/>
        /// <remarks>
        /// 若给定的实体数据已经存在于数据库中,抛出 InvalidOperationException 异常。
        /// 若要更新给定的实体数据,请使用 UpdateUserAuthorizationEntity 方法。
        /// </remarks>
        public void AddUserAuthorizationEntity(UserAuthorizationEntity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }
            if (QueryUserAuthorizationEntity(entity.Username) != null)
            {
                throw new InvalidOperationException("给定的实体数据已经存在于数据库中。");
            }

            UserAuthorization.Add(entity);
            SaveChanges();
        }
Exemplo n.º 5
0
        /// <summary>
        /// 更新数据库中给定的用户权限实体数据。
        /// </summary>
        /// <param name="entity">要更新的实体数据。</param>
        /// <exception cref="ArgumentNullException"/>
        /// <exception cref="InvalidOperationException"/>
        /// <remarks>
        /// 若给定的实体数据未在数据库中找到,抛出 InvalidOperationException 异常。
        /// 若要将给定的实体数据添加至数据库中,请调用 AddAuthorizationEntity 方法。
        /// </remarks>
        public void UpdateUserAuthorizationEntity(UserAuthorizationEntity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            UserAuthorizationEntity targetEntity = QueryUserAuthorizationEntity(entity.Username);

            if (targetEntity == null)
            {
                // 给定的数据实体不在数据库中。
                throw new InvalidOperationException("给定的数据实体不在数据库中。");
            }

            // 复制给定的用户权限实体数据到数据库中。
            targetEntity.PasswordHash = new byte[entity.PasswordHash.Length];
            Buffer.BlockCopy(entity.PasswordHash, 0, targetEntity.PasswordHash, 0, entity.PasswordHash.Length);
            targetEntity.Group = entity.Group;

            // 更新数据库。
            SaveChanges();
        }
 private static ClaimsIdentity GetIdentity(UserAuthorizationEntity entity) => entity != null?AuthenticationUtils.GetClaimsIdentity(entity) : null;
Exemplo n.º 7
0
 public static ClaimsIdentity GetClaimsIdentity(UserAuthorizationEntity entity) => GetClaimsIdentity(new List <Claim>
 {
     new Claim(AuthenticationClaimName.UserId, entity.Id.ToString()),
     new Claim(AuthenticationClaimName.UserPermissions, JsonConvert.SerializeObject(entity.Permissions))
 });