@param array $book_structure
@param bool $has_permission

@return array
333403
protected function fixBookStructure( array $book_structure, $has_permission ) {

		$toc = [];

		$replacement_keys = [
			'ID' => 'id',
			'post_title' => 'title',
			'post_name' => 'slug',
			'post_author' => 'author',
			'post_status' => 'status',
		];

		$toc['front-matter'] = $this->fixFrontMatterStructure( $book_structure, $has_permission, $replacement_keys );
		$toc['parts'] = $this->fixPartChapterStructure( $book_structure, $has_permission, $replacement_keys );
		$toc['back-matter'] = $this->fixBackMatterStructure( $book_structure, $has_permission, $replacement_keys );

		return $toc;
	}
1116687
public function mayAll($permissions, &$failed = null)
    {
        $this->mayOne($permissions, $failed);

        return $failed instanceof Collection ? $failed->isEmpty() : false;
    }
544790
private function getRestrictedContentTypes(array $hasAccess): array
    {
        $restrictedContentTypesIds = $this->permissionChecker->getRestrictions($hasAccess, ContentTypeLimitation::class);
        if (empty($restrictedContentTypesIds)) {
            return [];
        }

        $restrictedContentTypesIdentifiers = [];
        $restrictedContentTypes = $this->contentTypeService->loadContentTypeList($restrictedContentTypesIds);
        foreach ($restrictedContentTypes as $restrictedContentType) {
            $restrictedContentTypesIdentifiers[] = $restrictedContentType->identifier;
        }

        return $restrictedContentTypesIdentifiers;
    }
1051159
public function canCreateInLocation(Location $location, $hasAccess): bool
    {
        if (is_bool($hasAccess)) {
            return $hasAccess;
        }
        $restrictedLocations = $this->getRestrictions($hasAccess, LocationLimitation::class);
        $canCreateInLocation = empty($restrictedLocations)
            ? true
            : \in_array($location->id, array_map('intval', $restrictedLocations), true);

        if (false === $canCreateInLocation) {
            return false;
        }

        $restrictedParentContentTypes = $this->getRestrictions($hasAccess, ParentContentTypeLimitation::class);
        $canCreateInParentContentType = empty($restrictedParentContentTypes)
            ? true
            : \in_array($location->contentInfo->contentTypeId, array_map('intval', $restrictedParentContentTypes), true);

        if (false === $canCreateInParentContentType) {
            return false;
        }

        $restrictedParentDepths = $this->getRestrictions($hasAccess, ParentDepthLimitation::class);
        $canCreateInParentDepth = empty($restrictedParentDepths)
            ? true
            : \in_array($location->depth, array_map('intval', $restrictedParentDepths), true);

        if (false === $canCreateInParentDepth) {
            return false;
        }

        $restrictedParentOwner = $this->getRestrictions($hasAccess, ParentOwnerLimitation::class);
        $canCreateInParentOwner = empty($restrictedParentOwner)
            ? true
            : $location->contentInfo->ownerId === $this->permissionResolver->getCurrentUserReference()->getUserId();

        if (false === $canCreateInParentOwner) {
            return false;
        }

        $restrictedSections = $this->getRestrictions($hasAccess, SectionLimitation::class);
        $canCreateInSection = empty($restrictedSections)
            ? true
            : \in_array($location->contentInfo->sectionId, array_map('intval', $restrictedSections), true);

        if (false === $canCreateInSection) {
            return false;
        }

        $restrictedParentUserGroups = $this->getRestrictions($hasAccess, ParentUserGroupLimitation::class);
        $canCreateInParentUserGroup = empty($restrictedParentUserGroups)
            ? true
            : $this->hasSameParentUserGroup($location);

        if (false === $canCreateInParentUserGroup) {
            return false;
        }

        $restrictedSubtrees = $this->getRestrictions($hasAccess, SubtreeLimitation::class);
        $canCreateInSubtree = empty($restrictedSubtrees)
            ? true
            : !empty(array_filter($restrictedSubtrees, function ($restrictedSubtree) use ($location) {
                return strpos($location->pathString, $restrictedSubtree) === 0;
            }));

        if (false === $canCreateInSubtree) {
            return false;
        }

        return true;
    }
1211070
public function scopePermission(Builder $query, $permissions): Builder
    {
        $permissions = $this->convertToPermissionModels($permissions);

        $rolesWithPermissions = array_unique(array_reduce($permissions, function ($result, $permission) {
            return array_merge($result, $permission->roles->all());
        }, []));

        return $query->where(function ($query) use ($permissions, $rolesWithPermissions) {
            $query->whereHas('permissions', function ($query) use ($permissions) {
                $query->where(function ($query) use ($permissions) {
                    foreach ($permissions as $permission) {
                        $query->orWhere(config('permission.table_names.permissions').'.id', $permission->id);
                    }
                });
            });
            if (count($rolesWithPermissions) > 0) {
                $query->orWhereHas('roles', function ($query) use ($rolesWithPermissions) {
                    $query->where(function ($query) use ($rolesWithPermissions) {
                        foreach ($rolesWithPermissions as $role) {
                            $query->orWhere(config('permission.table_names.roles').'.id', $role->id);
                        }
                    });
                });
            }
        });
    }
537366
public function can($permissionName, $params = [], $allowCaching = true)
    {
        // check for auth manager rbac
        // copied from \yii\web\User
        $auth = Yii::$app->getAuthManager();
        if ($auth) {
            if ($allowCaching && empty($params) && isset($this->permissionCache[$permissionName])) {
                return $this->permissionCache[$permissionName];
            }
            $access = $auth->checkAccess($this->getId(), $permissionName, $params);
            if ($allowCaching && empty($params)) {
                $this->permissionCache[$permissionName] = $access;
            }
            return $access;
        }

        // otherwise use our own custom permission (via the role table)
        return $this->role->checkPermission($permissionName);
    }
282073
public function providePermissions()
    {
        $permissions = array();
        $config = Config::inst()->get('PublishProvider', 'classes');

        foreach ($config as $class) {
            $subClasses = array_keys(ClassInfo::subclassesFor($class));
            
            foreach ($subClasses as $subClass) {
                foreach (array(
                        'publish',
                        'unpublish',
                        'archive',
                        'view',
                        'edit',
                        'delete',
                        'create') as $name) {
                    $permissions[$subClass . '_' . $name] = singleton($subClass)->singular_name(). ' ' . $name;
                }
            }
        }
        
        return $permissions;
    }
399330
public static List<Entry> permissionStructure(Permission permission) {
        List<Entry> entryList = new LinkedList<>();
        entryList.add(namedObject(DnFactory.permission(permission)));
        entryList.add(namedDescriptiveObject(DnFactory.permissionAction(permission), permission.getAction()));
        entryList
                .add(namedDescriptiveObject(DnFactory.permissionComponent(permission), permission.getComponentName()));
        return entryList;
    }
478274
protected function getPermissionChecks($permissions, array $context, array $typeKeys): array
    {
        // Extra permission check of the whole tree to catch errors
        try {
            $this->lpProxy->checkAccess($permissions, $context, false);
        } catch (\Exception $e) {
            return [[
            'permissions' => $permissions,
            'resolve' => false,
            'error' => $e->getMessage(),
            ], ];
        }

        $checks = [];

        if (is_array($permissions)) {
            foreach ($permissions as $key => $value) {
                $checks = array_merge($checks, $this->getPermissionChecksRecursive([$key => $value], $context, $typeKeys));
            }
            if (count($permissions) > 1) {
                $checks[] = [
                'permissions' => $permissions,
                'resolve' => $this->lpProxy->checkAccess($permissions, $context, false),
                ];
            }
        } else {
            $checks = array_merge($checks, $this->getPermissionChecksRecursive($permissions, $context, $typeKeys));
        }

        return $checks;
    }
139111
public function hasAny(array $permissions, bool $need_affiliation = true): bool
    {

        foreach ($permissions as $permission)
            if ($this->has($permission, $need_affiliation))
                return true;

        return false;
    }
1356144
private function getRestrictedContentTypesIdentifiers($hasAccess): array
    {
        if (!is_array($hasAccess)) {
            return [];
        }

        $restrictedContentTypesIds = $this->permissionChecker->getRestrictions($hasAccess, ContentTypeLimitation::class);

        if (empty($restrictedContentTypesIds)) {
            return [];
        }

        $restrictedContentTypesIdentifiers = [];
        foreach ($restrictedContentTypesIds as $restrictedContentTypeId) {
            // TODO: Change to `contentTypeService->loadContentTypeList($restrictedContentTypesIds)` after #2444 will be merged
            try {
                $identifier = $this->contentTypeService->loadContentType($restrictedContentTypeId)->identifier;
                $restrictedContentTypesIdentifiers[] = $identifier;
            } catch (NotFoundException $e) {
            }
        }

        return $restrictedContentTypesIdentifiers;
    }
119756
public function hasCallerPermission(Caller $caller, Permission $permission)
    {
        $query = $this->getTable()
            ->where('caller_type', $caller->getCallerType())
            ->where('caller_id', $caller->getCallerId())
            ->where('type', $permission->getType())
            ->where('action', $permission->getAction());

        if (is_null($permission->getResourceType())) {
            $query->whereNull('resource_type');
        } else {
            $query->where('resource_type', $permission->getResourceType());
        }

        if (is_null($permission->getResourceId())) {
            $query->whereNull('resource_id');
        } else {
            $query->where('resource_id', $permission->getResourceId());
        }

        return (bool) $query->first();
    }
415192
protected function has_system_permission($filepath, $permissions) {
        if (in_array('r', $permissions)) {
            if (!is_readable($filepath)) {
                Log::info('Not readable path "' . $filepath . '"');
                return false;
            };
        }
        if (in_array('w', $permissions)) {
            if (!is_writable($filepath)) {
                Log::info('Not writable path "' . $filepath . '"');
                return false;
            }
        }
        return true;
    }
847244
public function check_permissions() {
        if ($this->recordid) {
            if (data_isowner($this->recordid)) {
                return has_capability('mod/data:exportownentry', context_module::instance($this->cm->id));
            }
            return has_capability('mod/data:exportentry', context_module::instance($this->cm->id));
        }
        if ($this->has_export_config() && !$this->get_export_config('mineonly')) {
            return has_capability('mod/data:exportallentries', context_module::instance($this->cm->id));
        }
        return has_capability('mod/data:exportownentry', context_module::instance($this->cm->id));
    }
770142
public function hasAffiliationAndPermission($permission)
    {

        // TODO: An annoying change in the 3x migration introduced
        // and array based permission, which is stupid. Remove that
        // or plan better for it in 3.1

        $array_permission = $permission;

        if (! is_array($permission))
            $array_permission = (array) $permission;

        // Process entries for character
        if (array_filter($array_permission, [$this, 'permissionCharacterLookup']))
            return $this->hasCharacterPermission($array_permission);

        // Process entries for corporations
        if (array_filter($array_permission, [$this, 'permissionCorporationLookup']))
            return $this->hasCorporationPermission($array_permission);

        return false;
    }
281138
public function hasPermission($permission)
    {
        if (!is_array($this->permissionsMap)) {
            $permissionsMap = cache()->get('permissions:' . $this->id, null);
            if ($permissionsMap === null) { // Not in cache
                $this->permissionsMap = $this->buildPermissionsMap();
                cache()->forever('permissions:' . $this->id, $this->permissionsMap);
            } else {
                $this->permissionsMap = $permissionsMap;
            }
        }
        return in_array($permission, $this->permissionsMap);
    }
16199
public static function addPermissions(array $permissions)
    {
        $rows = [];

        foreach ($permissions as $permission => $groups) {
            foreach ((array) $groups as $group) {
                $rows[] = [
                    'group_id' => $group,
                    'permission' => $permission,
                ];
            }
        }

        return [
            'up' => function (Builder $schema) use ($rows) {
                $db = $schema->getConnection();

                foreach ($rows as $row) {
                    if ($db->table('group_permission')->where($row)->exists()) {
                        continue;
                    }

                    if ($db->table('groups')->where('id', $row['group_id'])->doesntExist()) {
                        continue;
                    }

                    $db->table('group_permission')->insert($row);
                }
            },

            'down' => function (Builder $schema) use ($rows) {
                $db = $schema->getConnection();

                foreach ($rows as $row) {
                    $db->table('group_permission')->where($row)->delete();
                }
            }
        ];
    }