Revamped the organisation approvals process & refactored some files
This commit is contained in:
@@ -1,111 +0,0 @@
|
||||
<?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 Illuminate\Database\Capsule\Manager as DB;
|
||||
use UserFrosting\Sprinkle\Core\Database\Models\Model;
|
||||
|
||||
/**
|
||||
* Base organisation approval class.
|
||||
*
|
||||
* Represents a pending approval request which is tied to a user/organisation pair (membership, registration etc)
|
||||
*
|
||||
* @author Craig Williams (https://avsdev.uk)
|
||||
*
|
||||
* @property int $requester_id
|
||||
* @property int $organisation_id
|
||||
* @property hash $token
|
||||
* @property bool $completed
|
||||
* @property datetime $expires_at
|
||||
* @property datetime $completed_at
|
||||
* @property int $approver_id
|
||||
*/
|
||||
|
||||
class BaseOrganisationApproval extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'requester_id',
|
||||
'organisation_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 user associated with this request of this approval.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function requester()
|
||||
{
|
||||
/** @var \UserFrosting\Sprinkle\Core\Util\ClassMapper $classMapper */
|
||||
$classMapper = static::$ci->classMapper;
|
||||
|
||||
return $this->belongsTo($classMapper->getClassMapping('user'), 'requester_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the organisation associated with this approval request.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\belongsTo
|
||||
*/
|
||||
public function organisation()
|
||||
{
|
||||
/** @var \UserFrosting\Sprinkle\Core\Util\ClassMapper $classMapper */
|
||||
$classMapper = static::$ci->classMapper;
|
||||
|
||||
return $this->belongsTo($classMapper->getClassMapping('organisation'), 'organisation_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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');
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Database\Capsule\Manager as DB;
|
||||
use UserFrosting\Sprinkle\Core\Database\Models\Model;
|
||||
use UserFrosting\Sprinkle\Organisations\Database\Models\Interfaces\OrganisationInterface;
|
||||
use UserFrosting\Sprinkle\Organisations\Repository\Interfaces\TokenOwnerInterface;
|
||||
|
||||
/**
|
||||
* Organisation Class.
|
||||
@@ -28,7 +29,7 @@ use UserFrosting\Sprinkle\Organisations\Database\Models\Interfaces\OrganisationI
|
||||
* @property timestamp $updated_at
|
||||
* @property timestamp $deleted_at
|
||||
*/
|
||||
class Organisation extends Model implements OrganisationInterface
|
||||
class Organisation extends Model implements OrganisationInterface, TokenOwnerInterface
|
||||
{
|
||||
use SoftDeletes;
|
||||
|
||||
@@ -46,6 +47,7 @@ class Organisation extends Model implements OrganisationInterface
|
||||
'slug',
|
||||
'name',
|
||||
'description',
|
||||
'registrant_id',
|
||||
'flag_approved',
|
||||
'deleted_at',
|
||||
];
|
||||
@@ -82,6 +84,25 @@ class Organisation extends Model implements OrganisationInterface
|
||||
*/
|
||||
public $timestamps = true;
|
||||
|
||||
/**
|
||||
* Get the mapping id, as per TokenOwnerInterface
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the user who registered this organisation
|
||||
*/
|
||||
public function registrant()
|
||||
{
|
||||
/** @var \UserFrosting\Sprinkle\Core\Util\ClassMapper $classMapper */
|
||||
$classMapper = static::$ci->classMapper;
|
||||
|
||||
return $this->hasOne($classMapper->getClassMapping('user'), 'id', 'registrant_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of members within this organisation.
|
||||
@@ -93,8 +114,7 @@ class Organisation extends Model implements OrganisationInterface
|
||||
|
||||
return $this
|
||||
->belongsToMany($classMapper->getClassMapping('user'), 'organisation_members', 'organisation_id', 'user_id')
|
||||
->where('flag_admin', false)
|
||||
->withTimestamps();
|
||||
->where('flag_admin', false);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -107,8 +127,7 @@ class Organisation extends Model implements OrganisationInterface
|
||||
|
||||
return $this
|
||||
->belongsToMany($classMapper->getClassMapping('user'), 'organisation_members', 'organisation_id', 'user_id')
|
||||
->where('flag_admin', true)
|
||||
->withTimestamps();
|
||||
->where('flag_admin', true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -127,7 +146,7 @@ class Organisation extends Model implements OrganisationInterface
|
||||
static::$ci->get('organisation.beforeDelete')($this);
|
||||
|
||||
// Remove all organisation tokens
|
||||
$classMapper->getClassMapping('organisation_approval')::where('organisation_id', $this->id)->delete();
|
||||
$classMapper->getClassMapping('organisation_approval')::where('owner_id', $this->id)->delete();
|
||||
|
||||
// Remove all member associations
|
||||
$this->members()->detach();
|
||||
|
||||
@@ -9,8 +9,7 @@
|
||||
|
||||
namespace UserFrosting\Sprinkle\Organisations\Database\Models;
|
||||
|
||||
use Illuminate\Database\Capsule\Manager as DB;
|
||||
use UserFrosting\Sprinkle\Organisations\Database\Models\BaseOrganisationApproval;
|
||||
use UserFrosting\Sprinkle\Core\Database\Models\Model;
|
||||
|
||||
/**
|
||||
* Organisation Approval Class.
|
||||
@@ -18,12 +17,111 @@ use UserFrosting\Sprinkle\Organisations\Database\Models\BaseOrganisationApproval
|
||||
* Represents a pending organisation approval request.
|
||||
*
|
||||
* @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 OrganisationApproval extends BaseOrganisationApproval
|
||||
class OrganisationApproval extends Model
|
||||
{
|
||||
/**
|
||||
* @var string The name of the table for the current model.
|
||||
*/
|
||||
protected $table = 'organisation_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()
|
||||
{
|
||||
return $this->organisation();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the organisation of this approval.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function organisation()
|
||||
{
|
||||
/** @var \UserFrosting\Sprinkle\Core\Util\ClassMapper $classMapper */
|
||||
$classMapper = static::$ci->classMapper;
|
||||
|
||||
return $this->belongsTo($classMapper->getClassMapping('organisation'), 'owner_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the requester of this approval.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function requester()
|
||||
{
|
||||
return $this->organisation->registrant();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +37,11 @@ class User extends UFUser
|
||||
/** @var \UserFrosting\Sprinkle\Core\Util\ClassMapper $classMapper */
|
||||
$classMapper = static::$ci->classMapper;
|
||||
|
||||
return $this->belongsToMany($classMapper->getClassMapping('organisation'), 'organisation_members', 'user_id', 'organisation_id')->orderBy('organisations.name', 'asc')->withPivot('flag_admin');
|
||||
return $this->belongsToMany(
|
||||
$classMapper->getClassMapping('organisation'), 'organisation_members', 'user_id', 'organisation_id'
|
||||
)
|
||||
->orderBy('organisations.name', 'asc')
|
||||
->withPivot('flag_admin');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,7 +57,16 @@ class User extends UFUser
|
||||
$classMapper = static::$ci->classMapper;
|
||||
|
||||
if ($hardDelete) {
|
||||
// TODO: remove user from organisations?
|
||||
$classMapper->getClassMapping('organisation')::query()
|
||||
->where('registrant_id', $this->id)
|
||||
->update(['registrant_id' => null]);
|
||||
|
||||
$classMapper->getClassMapping('organisation_approval')::query()
|
||||
->where('approver_id', $this->id)
|
||||
->update(['approver_id' => null]);
|
||||
|
||||
$this->organisations()->detach();
|
||||
$this->refresh();
|
||||
}
|
||||
|
||||
return parent::delete();
|
||||
|
||||
Reference in New Issue
Block a user