List existing organisations

This commit is contained in:
2022-01-21 10:56:43 +00:00
parent c26c65fa8c
commit 584aa1909e
13 changed files with 613 additions and 0 deletions

9
asset-bundles.json Normal file
View File

@@ -0,0 +1,9 @@
{
"bundle": {
"js/pages/organisations": {
"scripts": [
"avsdev/js/pages/organisations.js"
]
}
}
}

View File

@@ -0,0 +1,16 @@
/**
* Page-specific Javascript file. Should generally be included as a separate asset bundle in your page template.
* example: {{ assets.js('js/pages/sign-in-or-register') | raw }}
*
* This script depends on widgets/organisations.js, uf-table.js, moment.js, handlebars-helpers.js
*
* Target page: /organisations
*/
$(document).ready(function() {
$("#widget-organisations").ufTable({
dataUrl: site.uri.public + "/api/organisations",
useLoadingTransition: site.uf_table.use_loading_transition
});
});

22
locale/en_US/messages.php Normal file
View File

@@ -0,0 +1,22 @@
<?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)
*/
/**
* US English message token translations for the 'organisations' sprinkle.
*
* @author Craig Williams (https://avsdev.uk)
*/
return [
'ORGANISATION' => [
1 => 'Organisation',
2 => 'Organisations',
'PAGE_DESCRIPTION' => 'A listing of the organisations for your site. Provides management tools for editing and deleting organisations.',
],
];

24
routes/organisations.php Normal file
View File

@@ -0,0 +1,24 @@
<?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)
*/
use UserFrosting\Sprinkle\Core\Util\NoCache;
/*
* Routes for administrative organisation management.
*/
$app->group('/organisations', function () {
$this->get('', 'UserFrosting\Sprinkle\Organisations\Controller\OrganisationController:pageList')
->setName('uri_organisations');
})->add('authGuard')->add(new NoCache());
$app->group('/api/organisations', function () {
$this->get('', 'UserFrosting\Sprinkle\Organisations\Controller\OrganisationController:getList');
})->add('authGuard')->add(new NoCache());
// TODO: add route for accepting members

View 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');
}
}

View 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');
}
}

View 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;
}

View 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);
}
}
}

View 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;
});
}
}

View 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();
}
}

View File

@@ -0,0 +1,43 @@
{% block navigation %}
{% if checkAccess('uri_dashboard') %}
<li>
<a href="{{site.uri.public}}/dashboard"><i class="fas fa-tachometer-alt fa-fw"></i> <span>{{ translate("DASHBOARD") }}</span></a>
</li>
{% endif %}
{% if checkAccess('uri_users') %}
<li>
<a href="{{site.uri.public}}/users"><i class="fas fa-user fa-fw"></i> <span>{{ translate("USER", 2) }}</span></a>
</li>
{% elseif checkAccess('uri_group', {
'group': current_user.group
}) %}
<li>
<a href="{{site.uri.public}}/groups/g/{{current_user.group.slug}}"><i class="{{current_user.group.icon}} fa-fw"></i> <span>{{ translate("GROUP.MANAGE") }}</span></a>
</li>
{% endif %}
{% if checkAccess('uri_activities') %}
<li>
<a href="{{site.uri.public}}/activities"><i class="fas fa-tasks fa-fw"></i> <span>{{ translate("ACTIVITY", 2) }}</span></a>
</li>
{% endif %}
{% if checkAccess('uri_roles') %}
<li>
<a href="{{site.uri.public}}/roles"><i class="fas fa-id-card fa-fw"></i> <span>{{ translate("ROLE", 2) }}</span></a>
</li>
{% endif %}
{% if checkAccess('uri_permissions') %}
<li>
<a href="{{site.uri.public}}/permissions"><i class="fas fa-key fa-fw"></i> <span>{{ translate("PERMISSION", 2) }}</span></a>
</li>
{% endif %}
{% if checkAccess('uri_groups') %}
<li>
<a href="{{site.uri.public}}/groups"><i class="fas fa-users fa-fw"></i> <span>{{ translate("GROUP", 2) }}</span></a>
</li>
{% endif %}
{% if checkAccess('uri_organisations') %}
<li>
<a href="{{site.uri.public}}/organisations"><i class="fas fa-users fa-fw"></i> <span>{{ translate("ORGANISATION", 2) }}</span></a>
</li>
{% endif %}
{% endblock %}

View File

@@ -0,0 +1,45 @@
{% extends "pages/abstract/dashboard.html.twig" %}
{% block stylesheets_page %}
<!-- Page-specific CSS asset bundle -->
{{ assets.css('css/form-widgets') | raw }}
{% endblock %}
{# Overrides blocks in head of base template #}
{% block page_title %}{{ translate("ORGANISATION", 2) }}{% endblock %}
{% block page_description %}{{ translate("ORGANISATION.PAGE_DESCRIPTION") }}{% endblock %}
{% block body_matter %}
<div class="row">
<div class="col-md-12">
<div id="widget-organisations" class="box box-primary">
<div class="box-header">
<h3 class="box-title"><i class="fas fa-fw fa-users"></i> {{translate('ORGANISATION', 2)}}</h3>
{% include "tables/table-tool-menu.html.twig" %}
</div>
<div class="box-body">
{% include "tables/organisations.html.twig" with {
"table" : {
"id" : "table-organisations"
}
}
%}
</div>
</div>
</div>
</div>
{% endblock %}
{% block scripts_page %}
<!-- Include validation rules -->
<script>
{% include "pages/partials/page.js.twig" %}
</script>
<!-- Include form widgets JS -->
{{ assets.js('js/form-widgets') | raw }}
<!-- Include page-specific JS -->
{{ assets.js('js/pages/organisations') | raw }}
{% endblock %}

View File

@@ -0,0 +1,59 @@
{# This partial template renders a table of organisations, to be populated with rows via an AJAX request.
# This extends a generic template for paginated tables.
#
# Note that this template contains a "skeleton" table with an empty table body, and then a block of Handlebars templates which are used
# to render the table cells with the data from the AJAX request.
#}
{% extends "tables/table-paginated.html.twig" %}
{% block table %}
<table id="{{table.id}}" class="tablesorter table table-bordered table-hover table-striped" data-sortlist="{{table.sortlist}}">
<thead>
<tr>
<th class="sorter-metatext" data-column-name="name" data-column-template="#organisation-table-column-info" data-priority="1">{{translate('ORGANISATION')}} <i class="fas fa-sort"></i></th>
<th class="sorter-metatext" data-column-name="description" data-column-template="#organisation-table-column-description" data-priority="2">{{translate("DESCRIPTION")}} <i class="fas fa-sort"></i></th>
<th data-column-template="#organisation-table-column-actions" data-sorter="false" data-filter="false" data-priority="1">{{translate("ACTIONS")}}</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
{% endblock %}
{% block table_cell_templates %}
{# This contains a series of <script> blocks, each of which is a client-side Handlebars template.
# Note that these are NOT Twig templates, although the syntax is similar. We wrap them in the `verbatim` tag,
# so that Twig will output them directly into the DOM instead of trying to treat them like Twig templates.
#
# These templates require handlebars-helpers.js, moment.js
#}
{% verbatim %}
<script id="organisation-table-column-info" type="text/x-handlebars-template">
<td data-text="{{row.name}}">
<strong>
<a href="{{site.uri.public}}/organisations/o/{{row.slug}}">{{row.name}}</a>
</strong>
</td>
</script>
<script id="organisation-table-column-description" type="text/x-handlebars-template">
<td>
{{row.description}}
</td>
</script>
<script id="organisation-table-column-actions" type="text/x-handlebars-template">
<td>
<div class="btn-organisation">
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
{% endverbatim %}{{translate("ACTIONS")}}{% verbatim %}
<span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-menu-right" role="menu">
</ul>
</div>
</td>
</script>
{% endverbatim %}
{% endblock %}