Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Test
on: [push]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
include:
- php-version: 7.2
phpunit: 7.5
mysql: 5.7.33
- php-version: 8.0
phpunit: 9.5
mysql: 8.0.23
services:
mysql:
image: mysql:${{ matrix.mysql }}
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: processgraphql
MYSQL_USER: processgraphql
MYSQL_PASSWORD: processgraphql
ports:
- 3306:3306
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup Node
uses: actions/setup-node@v2
with:
node-version: '14'
check-latest: true

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '${{ matrix.php-version }}'
tools: composer

- name: Dependencies
run: |
rm composer.lock
composer require phpunit/phpunit ^${{ matrix.phpunit }} --dev
composer update --with-all-dependencies
npm install

- name: Test
run: npm test
29 changes: 0 additions & 29 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ProcessGraphQL

[![Travis-CI Status][travis-ci-badge]][travis-ci]
[![Test Status](https://github.com/dadish/ProcessGraphQL/workflows/Test/badge.svg)](https://github.com/dadish/ProcessGraphQL/actions)

[GraphQL][graphql] for [ProcessWire][pw].

Expand Down
56 changes: 33 additions & 23 deletions src/Permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

class Permissions
{
public const pageAddPermission = 'page-add';
public const pageCreatePermission = 'page-create';
public const pageDeletePermission = 'page-delete';
public const pageEditPermission = 'page-edit';
public const pageMovePermission = 'page-move';
public const pageViewPermission = 'page-view';
public const pageEditCreatedPermission = 'page-edit-created';
public const pageEditTrashCreatedPermission = 'page-edit-trash-created';
public const pageAddPermission = "page-add";
public const pageCreatePermission = "page-create";
public const pageDeletePermission = "page-delete";
public const pageEditPermission = "page-edit";
public const pageMovePermission = "page-move";
public const pageViewPermission = "page-view";
public const pageEditCreatedPermission = "page-edit-created";
public const pageEditTrashCreatedPermission = "page-edit-trash-created";

/**
* Checks if the page using this template can be viewed by the current user.
Expand Down Expand Up @@ -60,23 +60,27 @@ public static function canCreate(Template $template)
}

// can't create if allowed parents are not legal
if ($template->noParents == 0 && count($template->parentTemplates)) {

if (!$template->noParents && count($template->parentTemplates)) {
// filter out the parents that has noChildren checked or
// has configured childTemplates without the target template
$parentTemplates = array_filter($template->parentTemplates, function ($templateId) use ($template) {
$parentTemplates = array_filter($template->parentTemplates, function (
$templateId
) use ($template) {
$parentTemplate = Utils::templates()->get($templateId);
if ($parentTemplate->noChildren) {
return false;
}
if (count($parentTemplate->childTemplates) && !in_array($template->id, $parentTemplate->childTemplates)) {
if (
count($parentTemplate->childTemplates) &&
!in_array($template->id, $parentTemplate->childTemplates)
) {
return false;
}
return true;
});

// get templates that user can add pages to
$addTemplates = self::getAddTemplates()->explode('id');
$addTemplates = self::getAddTemplates()->explode("id");
if (!count(array_intersect($addTemplates, $parentTemplates))) {
return false;
}
Expand Down Expand Up @@ -219,7 +223,11 @@ public static function canAdd(Template $template)

// can't add a page if the allowed childTemplates are not legal
if ($template->noChildren == 0 && count($template->childTemplates)) {
if (!count(array_intersect(self::getTemplateIds(), $template->childTemplates))) {
if (
!count(
array_intersect(self::getTemplateIds(), $template->childTemplates)
)
) {
return false;
}
}
Expand Down Expand Up @@ -251,7 +259,7 @@ public static function canAdd(Template $template)
*/
public static function canEditField(Field $field, Template $template)
{
return self::hasFieldPermission('edit', $field, $template);
return self::hasFieldPermission("edit", $field, $template);
}

/**
Expand All @@ -263,7 +271,7 @@ public static function canEditField(Field $field, Template $template)
*/
public static function canViewField(Field $field, Template $template)
{
return self::hasFieldPermission('view', $field, $template);
return self::hasFieldPermission("view", $field, $template);
}

/**
Expand All @@ -274,8 +282,11 @@ public static function canViewField(Field $field, Template $template)
* @param Template $template The context of the field.
* @return boolean Returns true if user has rights and false otherwise
*/
public static function hasFieldPermission(string $permission, Field $field, Template $template)
{
public static function hasFieldPermission(
string $permission,
Field $field,
Template $template
) {
$user = Utils::user();

// can view/edit a field if superuser
Expand All @@ -289,7 +300,7 @@ public static function hasFieldPermission(string $permission, Field $field, Temp
return false;
}

$roles = $permission . 'Roles';
$roles = $permission . "Roles";

foreach ($user->roles as $role) {
if (in_array($role->id, $field->$roles)) {
Expand All @@ -300,7 +311,6 @@ public static function hasFieldPermission(string $permission, Field $field, Temp
return false;
}


/**
* Tells if the template or field has access control defined.
*
Expand All @@ -309,7 +319,7 @@ public static function hasFieldPermission(string $permission, Field $field, Temp
*/
public static function definesAccess($context)
{
return (boolean) $context->useRoles;
return (bool) $context->useRoles;
}

/**
Expand All @@ -320,7 +330,7 @@ public static function definesAccess($context)
public static function getTemplates()
{
$templates = Utils::templates();
$legalTemplateNames = implode('|', Utils::module()->legalTemplates);
$legalTemplateNames = implode("|", Utils::module()->legalTemplates);
return $templates->find("name=$legalTemplateNames");
}

Expand All @@ -331,7 +341,7 @@ public static function getTemplates()
*/
public static function getTemplateIds()
{
return array_merge([], self::getTemplates()->explode('id'));
return array_merge([], self::getTemplates()->explode("id"));
}

public static function filterTemplatesByPermission($predicator)
Expand Down
17 changes: 14 additions & 3 deletions test/Field/Mutation/CreatePage/CaseTwoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,25 @@ public function testValue()
$res = self::execute($query, $variables);
$newBuildingSky = Utils::pages()->get("name=$name");
self::assertEquals(
1,
2,
count($res->errors),
"createSkyscraper does not exist if allowed parent page is not legal."
// "createSkyscraper does not exist if allowed parent page is not legal."
"Should have two errors."
);
self::assertStringContainsString(
"SkyscraperCreateInput",
$res->errors[0]->message,
"'SkyscraperCreateInput' type should not exist."
);
self::assertStringContainsString(
"createSkyscraper",
$res->errors[1]->message,
"'createSkyscraper' field should not exist."
);
self::assertInstanceOf(
NullPage::class,
$newBuildingSky,
"createSkyscraper does not create a page."
"'createSkyscraper' does not create a page."
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace ProcessWire\GraphQL\Test\Interface;
namespace ProcessWire\GraphQL\Test\Interfaces;

/**
* It supports page interfaces.
Expand Down
Loading