Пример #1
0
        private Dictionary <string, bool> GetUserPermissions(int userId)
        {
            var fld = UserPermissionRow.Fields;

            return(TwoLevelCache.GetLocalStoreOnly("UserPermissions:" + userId, TimeSpan.Zero, fld.GenerationKey, () =>
            {
                using (var connection = SqlConnections.NewByKey("Default"))
                {
                    var result = new Dictionary <string, bool>(StringComparer.OrdinalIgnoreCase);

                    connection.List <UserPermissionRow>(q => q
                                                        .Select(fld.PermissionKey)
                                                        .Select(fld.Granted)
                                                        .Where(new Criteria(fld.UserId) == userId))
                    .ForEach(x => result[x.PermissionKey] = x.Granted ?? true);

                    var implicitPermissions = new Repositories.UserPermissionRepository().ImplicitPermissions;
                    foreach (var pair in result.ToArray())
                    {
                        HashSet <string> list;
                        if (pair.Value && implicitPermissions.TryGetValue(pair.Key, out list))
                        {
                            foreach (var x in list)
                            {
                                result.Add(x, true);
                            }
                        }
                    }

                    return result;
                }
            }));
        }
Пример #2
0
        private HashSet <string> GetRolePermissions(int userId)
        {
            var fld = RolePermissionRow.Fields;

            return(TwoLevelCache.GetLocalStoreOnly("RolePermissions:" + userId, TimeSpan.Zero, fld.GenerationKey, () =>
            {
                using (var connection = SqlConnections.NewByKey("Default"))
                {
                    var result = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

                    connection.List <RolePermissionRow>(q => q
                                                        .Select(fld.PermissionKey)
                                                        .Where(new Criteria(fld.RoleId) == userId))
                    .ForEach(x => result.Add(x.PermissionKey));

                    var implicitPermissions = new Repositories.UserPermissionRepository().ImplicitPermissions;
                    foreach (var key in result.ToArray())
                    {
                        HashSet <string> list;
                        if (implicitPermissions.TryGetValue(key, out list))
                        {
                            foreach (var x in list)
                            {
                                result.Add(x);
                            }
                        }
                    }

                    return result;
                }
            }));
        }
        public SaveResponse Update(IUnitOfWork uow, RolePermissionUpdateRequest request)
        {
            Check.NotNull(request, "request");
            Check.NotNull(request.RoleID, "roleID");
            Check.NotNull(request.Permissions, "permissions");

            var roleID  = request.RoleID.Value;
            var oldList = new HashSet <string>(
                GetExisting(uow.Connection, roleID, request.Module, request.Submodule)
                .Select(x => x.PermissionKey), StringComparer.OrdinalIgnoreCase);

            var newList = new HashSet <string>(request.Permissions.ToList(),
                                               StringComparer.OrdinalIgnoreCase);

            var allowedKeys = new UserPermissionRepository()
                              .ListPermissionKeys()
                              .Entities.ToDictionary(x => x);

            if (newList.Any(x => !allowedKeys.ContainsKey(x)))
            {
                throw new AccessViolationException();
            }

            if (oldList.SetEquals(newList))
            {
                return(new SaveResponse());
            }

            foreach (var k in oldList)
            {
                if (newList.Contains(k))
                {
                    continue;
                }

                new SqlDelete(fld.TableName)
                .Where(
                    new Criteria(fld.RoleId) == roleID &
                    new Criteria(fld.PermissionKey) == k)
                .Execute(uow.Connection);
            }

            foreach (var k in newList)
            {
                if (oldList.Contains(k))
                {
                    continue;
                }

                uow.Connection.Insert(new MyRow
                {
                    RoleId        = roleID,
                    PermissionKey = k
                });
            }

            BatchGenerationUpdater.OnCommit(uow, fld.GenerationKey);
            BatchGenerationUpdater.OnCommit(uow, Entities.UserPermissionRow.Fields.GenerationKey);

            return(new SaveResponse());
        }