Exemplo n.º 1
0
 public async Task<IDictionary<string, Kiss4UserRight>> GetRightsOfUser(int userId)
 {
     var key = new UserRightsCacheKey(userId);
     return await _cache.GetOrCreateAsync(key, entry =>
     {
         entry.SlidingExpiration = _slidingExpiration;
         return _decoratee.GetRightsOfUser(userId);
     });
 }
Exemplo n.º 2
0
        public async Task <bool> UserHasRights(IEnumerable <Kiss4RightAttribute> expectedRights)
        {
            if (expectedRights.IsNullOrEmpty())
            {
                return(true);
            }

            var userId = _userIdProvider.UserId;

            if (userId == null)
            {
                throw new ArgumentNullException("userId");
            }

            if (_userIdProvider.IsUserAdmin || _userIdProvider.IsUserSuperAdmin)
            {
                // ToDo: handle queries that only SuperAdmin may execute
                return(true);
            }

            var userRights = await _userRightsProvider.GetRightsOfUser(userId.Value);

            foreach (var expectedRight in expectedRights)
            {
                if (userRights.TryGetValue(expectedRight.RightName, out var userRight))
                {
                    if (expectedRight.Insert && !userRight.MayInsert ||
                        expectedRight.Update && !userRight.MayUpdate ||
                        expectedRight.Delete && !userRight.MayDelete)
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }

            return(true);
        }