List existing organisations
This commit is contained in:
102
src/Controller/OrganisationController.php
Normal file
102
src/Controller/OrganisationController.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* AVSDev UF Organisations (https://avsdev.uk)
|
||||
*
|
||||
* @link https://git.avsdev.uk/avsdev/sprinkle-organisations
|
||||
* @license https://git.avsdev.uk/avsdev/sprinkle-organisations/blob/master/LICENSE.md (LGPL-3.0 License)
|
||||
*/
|
||||
|
||||
namespace UserFrosting\Sprinkle\Organisations\Controller;
|
||||
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use UserFrosting\Fortress\Adapter\JqueryValidationAdapter;
|
||||
use UserFrosting\Fortress\RequestDataTransformer;
|
||||
use UserFrosting\Fortress\RequestSchema;
|
||||
use UserFrosting\Fortress\ServerSideValidator;
|
||||
use UserFrosting\Sprinkle\Account\Database\Models\Group;
|
||||
use UserFrosting\Sprinkle\Core\Controller\SimpleController;
|
||||
use UserFrosting\Support\Exception\BadRequestException;
|
||||
use UserFrosting\Support\Exception\ForbiddenException;
|
||||
use UserFrosting\Support\Exception\NotFoundException;
|
||||
|
||||
/**
|
||||
* Controller class for organisation-related requests, including listing organisations, CRUD for organisations, etc.
|
||||
*
|
||||
* @author Craig Williams (https://avsdev.uk)
|
||||
*/
|
||||
class OrganisationController extends SimpleController
|
||||
{
|
||||
/**
|
||||
* Returns a list of Organisations.
|
||||
*
|
||||
* Generates a list of organisations, optionally paginated, sorted and/or filtered.
|
||||
* This page requires authentication.
|
||||
*
|
||||
* Request type: GET
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @param array $args
|
||||
*
|
||||
* @throws ForbiddenException If user is not authorized to access page
|
||||
*/
|
||||
public function getList(Request $request, Response $response, $args)
|
||||
{
|
||||
// GET parameters
|
||||
$params = $request->getQueryParams();
|
||||
|
||||
/** @var \UserFrosting\Sprinkle\Account\Authorize\AuthorizationManager $authorizer */
|
||||
$authorizer = $this->ci->authorizer;
|
||||
|
||||
/** @var \UserFrosting\Sprinkle\Account\Database\Models\Interfaces\UserInterface $currentUser */
|
||||
$currentUser = $this->ci->currentUser;
|
||||
|
||||
// Access-controlled page
|
||||
if (!$authorizer->checkAccess($currentUser, 'uri_organisations')) {
|
||||
throw new ForbiddenException();
|
||||
}
|
||||
|
||||
/** @var \UserFrosting\Sprinkle\Core\Util\ClassMapper $classMapper */
|
||||
$classMapper = $this->ci->classMapper;
|
||||
|
||||
$sprunje = $classMapper->createInstance('organisation_sprunje', $classMapper, $params);
|
||||
|
||||
// Be careful how you consume this data - it has not been escaped and contains untrusted user-supplied content.
|
||||
// For example, if you plan to insert it into an HTML DOM, you must escape it on the client side (or use client-side templating).
|
||||
return $sprunje->toResponse($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the organisation listing page.
|
||||
*
|
||||
* This page renders a table of groups, with dropdown menus for admin actions for each organisation.
|
||||
* Actions typically include: edit organisation, delete organisation.
|
||||
* This page requires authentication.
|
||||
*
|
||||
* Request type: GET
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @param array $args
|
||||
*
|
||||
* @throws ForbiddenException If user is not authorized to access page
|
||||
*/
|
||||
public function pageList(Request $request, Response $response, $args)
|
||||
{
|
||||
/** @var \UserFrosting\Sprinkle\Account\Authorize\AuthorizationManager $authorizer */
|
||||
$authorizer = $this->ci->authorizer;
|
||||
|
||||
/** @var \UserFrosting\Sprinkle\Account\Database\Models\Interfaces\UserInterface $currentUser */
|
||||
$currentUser = $this->ci->currentUser;
|
||||
|
||||
// Access-controlled page
|
||||
if (!$authorizer->checkAccess($currentUser, 'uri_organisations')) {
|
||||
throw new ForbiddenException();
|
||||
}
|
||||
|
||||
return $this->ci->view->render($response, 'pages/organisations.html.twig');
|
||||
}
|
||||
}
|
||||
56
src/Database/Migrations/v001/OrganisationsTable.php
Normal file
56
src/Database/Migrations/v001/OrganisationsTable.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* AVSDev UF Organisations (https://avsdev.uk)
|
||||
*
|
||||
* @link https://git.avsdev.uk/avsdev/sprinkle-organisations
|
||||
* @license https://git.avsdev.uk/avsdev/sprinkle-organisations/blob/master/LICENSE.md (LGPL-3.0 License)
|
||||
*/
|
||||
|
||||
namespace UserFrosting\Sprinkle\Organisations\Database\Migrations\v001;
|
||||
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use UserFrosting\Sprinkle\Organisations\Database\Models\Organisation;
|
||||
use UserFrosting\Sprinkle\Core\Database\Migration;
|
||||
use UserFrosting\Sprinkle\Core\Facades\Seeder;
|
||||
|
||||
/**
|
||||
* Organisations table migration
|
||||
* "Organisations" are intended to replace groups by providing logical grouping where users may be in none or more organisations.
|
||||
* Version 1.0.0.
|
||||
*
|
||||
* @author Craig Williams (https://avsdev.uk)
|
||||
*/
|
||||
class OrganisationsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
if (!$this->schema->hasTable('organisations')) {
|
||||
$this->schema->create('organisations', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('slug');
|
||||
$table->string('name');
|
||||
$table->text('description')->nullable();
|
||||
$table->softDeletes();
|
||||
$table->timestamps();
|
||||
|
||||
$table->engine = 'InnoDB';
|
||||
$table->collation = 'utf8_unicode_ci';
|
||||
$table->charset = 'utf8';
|
||||
$table->unique('slug');
|
||||
$table->index('slug');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
$this->schema->drop('organisations');
|
||||
}
|
||||
}
|
||||
60
src/Database/Models/Organisation.php
Normal file
60
src/Database/Models/Organisation.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* AVSDev UF Organisations (https://avsdev.uk)
|
||||
*
|
||||
* @link https://git.avsdev.uk/avsdev/sprinkle-organisations
|
||||
* @license https://git.avsdev.uk/avsdev/sprinkle-organisations/blob/master/LICENSE.md (LGPL-3.0 License)
|
||||
*/
|
||||
|
||||
namespace UserFrosting\Sprinkle\Organisations\Database\Models;
|
||||
|
||||
use UserFrosting\Sprinkle\Core\Database\Models\Model;
|
||||
|
||||
/**
|
||||
* Organisation Class.
|
||||
*
|
||||
* Represents a organisation object as stored in the database.
|
||||
*
|
||||
* @author Craig Williams (https://avsdev.uk)
|
||||
*
|
||||
* @property string $slug
|
||||
* @property string $name
|
||||
* @property string $description
|
||||
* @property timestamp $created_at
|
||||
* @property timestamp $updated_at
|
||||
* @property timestamp $deleted_at
|
||||
*/
|
||||
class Organisation extends Model
|
||||
{
|
||||
/**
|
||||
* @var string The name of the table for the current model.
|
||||
*/
|
||||
protected $table = 'organisations';
|
||||
|
||||
/**
|
||||
* Fields that should be mass-assignable when creating a new Organisation.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $fillable = [
|
||||
'slug',
|
||||
'name',
|
||||
'description',
|
||||
'deleted_at',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be mutated to dates.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $dates = [
|
||||
'deleted_at',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var bool Enable timestamps for this class.
|
||||
*/
|
||||
public $timestamps = true;
|
||||
}
|
||||
91
src/Database/Seeds/OrganisationPermissions.php
Normal file
91
src/Database/Seeds/OrganisationPermissions.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* AVSDev UF Organisations (https://avsdev.uk)
|
||||
*
|
||||
* @link https://git.avsdev.uk/avsdev/sprinkle-organisations
|
||||
* @license https://git.avsdev.uk/avsdev/sprinkle-organisations/blob/master/LICENSE.md (LGPL-3.0 License)
|
||||
*/
|
||||
|
||||
namespace UserFrosting\Sprinkle\Organisations\Database\Seeds;
|
||||
|
||||
use UserFrosting\Sprinkle\Account\Database\Models\Permission;
|
||||
use UserFrosting\Sprinkle\Account\Database\Models\Role;
|
||||
use UserFrosting\Sprinkle\Core\Database\Seeder\BaseSeed;
|
||||
use UserFrosting\Sprinkle\Core\Facades\Seeder;
|
||||
|
||||
/**
|
||||
* Seeder for the permissions related to organisations.
|
||||
*
|
||||
* @author Craig Williams (https://avsdev.uk)
|
||||
*/
|
||||
class OrganisationPermissions extends BaseSeed
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
// We require the default roles
|
||||
Seeder::execute('DefaultRoles');
|
||||
|
||||
// Get and save permissions
|
||||
$permissions = $this->getPermissions();
|
||||
$this->savePermissions($permissions);
|
||||
|
||||
// Add default mappings to permissions
|
||||
$this->syncPermissionsRole($permissions);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array Permissions to seed
|
||||
*/
|
||||
protected function getPermissions()
|
||||
{
|
||||
return [
|
||||
'uri_organisations' => new Permission([
|
||||
'slug' => 'uri_organisations',
|
||||
'name' => 'Organisation management page',
|
||||
'conditions' => 'always()',
|
||||
'description' => 'View a page containing a list of organisations.',
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Save permissions.
|
||||
*
|
||||
* @param array $permissions
|
||||
*/
|
||||
protected function savePermissions(array &$permissions)
|
||||
{
|
||||
foreach ($permissions as $slug => $permission) {
|
||||
|
||||
// Trying to find if the permission already exist
|
||||
$existingPermission = Permission::where(['slug' => $permission->slug, 'conditions' => $permission->conditions])->first();
|
||||
|
||||
// Don't save if already exist, use existing permission reference
|
||||
// otherwise to re-sync permissions and roles
|
||||
if ($existingPermission == null) {
|
||||
$permission->save();
|
||||
} else {
|
||||
$permissions[$slug] = $existingPermission;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync permissions with default roles.
|
||||
*
|
||||
* @param array $permissions
|
||||
*/
|
||||
protected function syncPermissionsRole(array $permissions)
|
||||
{
|
||||
$roleSiteAdmin = Role::where('slug', 'site-admin')->first();
|
||||
if ($roleSiteAdmin) {
|
||||
$roleSiteAdmin->permissions()->sync([
|
||||
$permissions['uri_organisations'],
|
||||
], false);
|
||||
}
|
||||
}
|
||||
}
|
||||
44
src/ServicesProvider/ServicesProvider.php
Normal file
44
src/ServicesProvider/ServicesProvider.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* AVSDev UF Organisations (https://avsdev.uk)
|
||||
*
|
||||
* @link https://git.avsdev.uk/avsdev/sprinkle-organisations
|
||||
* @license https://git.avsdev.uk/avsdev/sprinkle-organisations/blob/master/LICENSE.md (LGPL-3.0 License)
|
||||
*/
|
||||
|
||||
namespace UserFrosting\Sprinkle\Organisations\ServicesProvider;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
|
||||
/**
|
||||
* Registers services for the organisation sprinkle.
|
||||
*
|
||||
* @author Craig Williams (https://avsdev.uk)
|
||||
*/
|
||||
class ServicesProvider
|
||||
{
|
||||
/**
|
||||
* Register UserFrosting's organisation services.
|
||||
*
|
||||
* @param ContainerInterface $container A DI container implementing ArrayAccess and psr-container.
|
||||
*/
|
||||
public function register(ContainerInterface $container)
|
||||
{
|
||||
/*
|
||||
* Extend the 'classMapper' service to register sprunje and model classes.
|
||||
*
|
||||
* Mappings added: 'organisation', 'organisation_sprunje'
|
||||
*
|
||||
* @return \UserFrosting\Sprinkle\Core\Util\ClassMapper
|
||||
*/
|
||||
$container->extend('classMapper', function ($classMapper, $c) {
|
||||
$classMapper->setClassMapping('organisation', 'UserFrosting\Sprinkle\Organisations\Database\Models\Organisation');
|
||||
$classMapper->setClassMapping('organisation_sprunje', 'UserFrosting\Sprinkle\Organisations\Sprunje\OrganisationSprunje');
|
||||
|
||||
return $classMapper;
|
||||
});
|
||||
}
|
||||
}
|
||||
42
src/Sprunje/OrganisationSprunje.php
Normal file
42
src/Sprunje/OrganisationSprunje.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* AVSDev UF Organisations (https://avsdev.uk)
|
||||
*
|
||||
* @link https://git.avsdev.uk/avsdev/sprinkle-organisations
|
||||
* @license https://git.avsdev.uk/avsdev/sprinkle-organisations/blob/master/LICENSE.md (LGPL-3.0 License)
|
||||
*/
|
||||
|
||||
namespace UserFrosting\Sprinkle\Organisations\Sprunje;
|
||||
|
||||
use UserFrosting\Sprinkle\Core\Sprunje\Sprunje;
|
||||
|
||||
/**
|
||||
* OrganisationSprunje.
|
||||
*
|
||||
* Implements Sprunje for the organisations API.
|
||||
*
|
||||
* @author Craig Williams (https://avsdev.uk)
|
||||
*/
|
||||
class OrganisationSprunje extends Sprunje
|
||||
{
|
||||
protected $name = 'groups';
|
||||
|
||||
protected $sortable = [
|
||||
'name',
|
||||
'description',
|
||||
];
|
||||
|
||||
protected $filterable = [
|
||||
'name',
|
||||
'description',
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function baseQuery()
|
||||
{
|
||||
return $this->classMapper->createInstance('organisation')->newQuery();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user