Created a membership approval token repository and required token table & model
This commit is contained in:
@@ -0,0 +1,61 @@
|
|||||||
|
<?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\Core\Database\Migration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Organisation Membership Approvals table migration
|
||||||
|
* Manages requests for organisation membership join requests.
|
||||||
|
* Version 1.0.0.
|
||||||
|
*
|
||||||
|
* @author Craig Williams (https://avsdev.uk)
|
||||||
|
*/
|
||||||
|
class OrganisationMembershipApprovalsTable extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
if (!$this->schema->hasTable('organisation_membership_approvals')) {
|
||||||
|
$this->schema->create('organisation_membership_approvals', function (Blueprint $table) {
|
||||||
|
$table->increments('id');
|
||||||
|
$table->integer('requester_id')->unsigned();
|
||||||
|
$table->integer('organisation_id')->unsigned();
|
||||||
|
$table->string('hash');
|
||||||
|
$table->boolean('completed')->default(0);
|
||||||
|
$table->timestamp('expires_at')->nullable();
|
||||||
|
$table->timestamp('completed_at')->nullable();
|
||||||
|
$table->integer('approver_id')->unsigned()->nullable();
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->engine = 'InnoDB';
|
||||||
|
$table->collation = 'utf8_unicode_ci';
|
||||||
|
$table->charset = 'utf8';
|
||||||
|
$table->foreign('requester_id')->references('id')->on('users');
|
||||||
|
$table->foreign('approver_id')->references('id')->on('users');
|
||||||
|
$table->foreign('organisation_id')->references('id')->on('organisations');
|
||||||
|
$table->index('requester_id');
|
||||||
|
$table->index('approver_id');
|
||||||
|
$table->index('hash');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
$this->schema->drop('organisation_membership_approvals');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
<?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\v002;
|
||||||
|
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Database\Capsule\Manager as DB;
|
||||||
|
use UserFrosting\Sprinkle\Core\Database\Migration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Organisation Membership Approvals table migration
|
||||||
|
* Manages requests for organisation membership join requests.
|
||||||
|
* Version 1.0.0.
|
||||||
|
*
|
||||||
|
* @author Craig Williams (https://avsdev.uk)
|
||||||
|
*/
|
||||||
|
class UpdateOrganisationMembershipApprovalsTable extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
if ($this->schema->hasTable('organisation_membership_approvals')) {
|
||||||
|
DB::table('organisation_membership_approvals')->delete();
|
||||||
|
$this->schema->table('organisation_membership_approvals', function (Blueprint $table) {
|
||||||
|
$table->dropForeign(['requester_id']);
|
||||||
|
$table->dropForeign(['organisation_id']);
|
||||||
|
$table->dropIndex(['requester_id']);
|
||||||
|
$table->dropColumn(['requester_id']);
|
||||||
|
$table->dropColumn(['organisation_id']);
|
||||||
|
|
||||||
|
$table->integer('owner_id')->unsigned();
|
||||||
|
$table->index('owner_id');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
if ($this->schema->hasTable('organisation_membership_approvals')) {
|
||||||
|
DB::table('organisation_membership_approvals')->delete();
|
||||||
|
$this->schema->table('organisation_membership_approvals', function (Blueprint $table) {
|
||||||
|
$table->dropIndex(['owner_id']);
|
||||||
|
$table->dropColumn('owner_id')->unsigned();
|
||||||
|
|
||||||
|
$table->integer('requester_id')->unsigned();
|
||||||
|
$table->integer('organisation_id')->unsigned();
|
||||||
|
$table->index('requester_id');
|
||||||
|
$table->foreign('requester_id')->references('id')->on('users');
|
||||||
|
$table->foreign('organisation_id')->references('id')->on('organisations');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
128
src/Database/Models/OrganisationMembershipApproval.php
Normal file
128
src/Database/Models/OrganisationMembershipApproval.php
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
<?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 Memebrship Approval Class.
|
||||||
|
*
|
||||||
|
* Represents a pending request by a user to join an organisation.
|
||||||
|
*
|
||||||
|
* @author Craig Williams (https://avsdev.uk)
|
||||||
|
*
|
||||||
|
* @property int $owner_id
|
||||||
|
* @property hash $token
|
||||||
|
* @property bool $completed
|
||||||
|
* @property datetime $expires_at
|
||||||
|
* @property datetime $completed_at
|
||||||
|
* @property int $approver_id
|
||||||
|
*/
|
||||||
|
|
||||||
|
class OrganisationMembershipApproval
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var string The name of the table for the current model.
|
||||||
|
*/
|
||||||
|
protected $table = 'organisation_membership_approvals';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fields that should be mass-assignable when creating a new Organisation.
|
||||||
|
*
|
||||||
|
* @var string[]
|
||||||
|
*/
|
||||||
|
protected $fillable = [
|
||||||
|
'owner_id',
|
||||||
|
'hash',
|
||||||
|
'completed',
|
||||||
|
'expires_at',
|
||||||
|
'completed_at',
|
||||||
|
'approver_id',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var bool Enable timestamps for Verifications.
|
||||||
|
*/
|
||||||
|
public $timestamps = true;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string Stores the raw (unhashed) token when created, so that it can be emailed out to the user. NOT persisted.
|
||||||
|
*/
|
||||||
|
protected $token;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getToken()
|
||||||
|
{
|
||||||
|
return $this->token;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $value
|
||||||
|
*
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function setToken($value)
|
||||||
|
{
|
||||||
|
$this->token = $value;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the owner.
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||||
|
*/
|
||||||
|
public function owner()
|
||||||
|
{
|
||||||
|
/** @var \UserFrosting\Sprinkle\Core\Util\ClassMapper $classMapper */
|
||||||
|
$classMapper = static::$ci->classMapper;
|
||||||
|
|
||||||
|
return $this->belongsTo($classMapper->getClassMapping('organisation_member'), 'map_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the organisation of this approval.
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||||
|
*/
|
||||||
|
public function organisation()
|
||||||
|
{
|
||||||
|
return $this->owner()->organisation();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the requester of this approval.
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||||
|
*/
|
||||||
|
public function requester()
|
||||||
|
{
|
||||||
|
return $this->owner()->user();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the user associated with this approval or denial of this request.
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Database\Eloquent\Relations\belongsTo
|
||||||
|
*/
|
||||||
|
public function approver()
|
||||||
|
{
|
||||||
|
/** @var \UserFrosting\Sprinkle\Core\Util\ClassMapper $classMapper */
|
||||||
|
$classMapper = static::$ci->classMapper;
|
||||||
|
|
||||||
|
return $this->belongsTo($classMapper->getClassMapping('user'), 'approver_id');
|
||||||
|
}
|
||||||
|
}
|
||||||
53
src/Repository/OrganisationMembershipApprovalRepository.php
Normal file
53
src/Repository/OrganisationMembershipApprovalRepository.php
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
<?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\Repository;
|
||||||
|
|
||||||
|
use UserFrosting\Sprinkle\Core\Util\ClassMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Token repository organisation membership join requests.
|
||||||
|
*
|
||||||
|
* @author Craig Williams (https://avsdev.uk)
|
||||||
|
*/
|
||||||
|
class OrganisationMembershipApprovalRepository extends BasicTokenRepository
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected $modelIdentifier = 'organisation_membership_approval';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function updateTokenOwner($owner_id, $model, $args)
|
||||||
|
{
|
||||||
|
$memberMap = $this->classMapper->getClassMapping('organisation_member')::findUnique($owner_id, 'map_id', false);
|
||||||
|
|
||||||
|
if (!$memberMap) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If specified, recored the approver. This assumes the model has an approver_id field which it may not...
|
||||||
|
if ($args['approver_id']) {
|
||||||
|
$model->approver_id = $args['approver_id'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($args['approved']) {
|
||||||
|
// Mark the request as approved
|
||||||
|
$memberMap->flag_approved = true;
|
||||||
|
$memberMap->save();
|
||||||
|
} else {
|
||||||
|
// Delete the request
|
||||||
|
$memberMap->delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,6 +20,7 @@ use UserFrosting\Sprinkle\Core\Log\MixedFormatter;
|
|||||||
use UserFrosting\Sprinkle\Organisations\Database\Models\Interfaces\OrganisationInterface;
|
use UserFrosting\Sprinkle\Organisations\Database\Models\Interfaces\OrganisationInterface;
|
||||||
use UserFrosting\Sprinkle\Organisations\Twig\OrganisationsExtension;
|
use UserFrosting\Sprinkle\Organisations\Twig\OrganisationsExtension;
|
||||||
use UserFrosting\Sprinkle\Organisations\Repository\OrganisationApprovalRepository;
|
use UserFrosting\Sprinkle\Organisations\Repository\OrganisationApprovalRepository;
|
||||||
|
use UserFrosting\Sprinkle\Organisations\Repository\OrganisationMembershipApprovalRepository;
|
||||||
use UserFrosting\Sprinkle\Organisations\Authorize\AuthorizationManager;
|
use UserFrosting\Sprinkle\Organisations\Authorize\AuthorizationManager;
|
||||||
|
|
||||||
|
|
||||||
@@ -49,6 +50,7 @@ class ServicesProvider
|
|||||||
$classMapper->setClassMapping('organisation_approval', 'UserFrosting\Sprinkle\Organisations\Database\Models\OrganisationApproval');
|
$classMapper->setClassMapping('organisation_approval', 'UserFrosting\Sprinkle\Organisations\Database\Models\OrganisationApproval');
|
||||||
$classMapper->setClassMapping('organisation_sprunje', 'UserFrosting\Sprinkle\Organisations\Sprunje\OrganisationSprunje');
|
$classMapper->setClassMapping('organisation_sprunje', 'UserFrosting\Sprinkle\Organisations\Sprunje\OrganisationSprunje');
|
||||||
$classMapper->setClassMapping('organisation_member', 'UserFrosting\Sprinkle\Organisations\Database\Models\OrganisationMember');
|
$classMapper->setClassMapping('organisation_member', 'UserFrosting\Sprinkle\Organisations\Database\Models\OrganisationMember');
|
||||||
|
$classMapper->setClassMapping('organisation_membership_approval', 'UserFrosting\Sprinkle\Organisations\Database\Models\OrganisationMembershipApproval');
|
||||||
$classMapper->setClassMapping('user', 'UserFrosting\Sprinkle\Organisations\Database\Models\User');
|
$classMapper->setClassMapping('user', 'UserFrosting\Sprinkle\Organisations\Database\Models\User');
|
||||||
$classMapper->setClassMapping('user_sprunje', 'UserFrosting\Sprinkle\Organisations\Sprunje\UserSprunje');
|
$classMapper->setClassMapping('user_sprunje', 'UserFrosting\Sprinkle\Organisations\Sprunje\UserSprunje');
|
||||||
|
|
||||||
@@ -181,5 +183,19 @@ class ServicesProvider
|
|||||||
|
|
||||||
return $repo;
|
return $repo;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Repository for membership approval requests.
|
||||||
|
*
|
||||||
|
* @return \UserFrosting\Sprinkle\Organisations\Repository\OrganisationMembershipApprovalRepository
|
||||||
|
*/
|
||||||
|
$container['repoOrganisationMembershipApproval'] = function ($c) {
|
||||||
|
$classMapper = $c->classMapper;
|
||||||
|
$config = $c->config;
|
||||||
|
|
||||||
|
$repo = new OrganisationMembershipApprovalRepository($classMapper, $config['verification.algorithm'], $c['tokenLogger'], $config['debug.tokens']);
|
||||||
|
|
||||||
|
return $repo;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user