Split pending organisations into a separate list and default to only returning organisations a user is an accepted member of (Fixes #1)
This commit is contained in:
@@ -80,7 +80,7 @@ class OrganisationMembersController extends SimpleController
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
if ($config['organisation']['membership']['single_membership'] && $currentUser->organisations()->count() > 0) {
|
||||
if ($config['organisation']['membership']['single_membership'] && $currentUser->organisations(true)->count() > 0) {
|
||||
$ms->addMessageTranslated('danger', 'ORGANISATION.JOIN_REQUEST.ALREADY_IN_ONE');
|
||||
return $response->withJson([], 400);
|
||||
}
|
||||
@@ -294,7 +294,7 @@ class OrganisationMembersController extends SimpleController
|
||||
|
||||
// Begin transaction - DB will be rolled back if an exception occurs
|
||||
Capsule::transaction(function () use ($organisation, $currentUser) {
|
||||
$currentUser->organisations()->detach($organisation->id);
|
||||
$currentUser->organisations(true)->detach($organisation->id);
|
||||
|
||||
// Create activity record
|
||||
$this->ci->userActivityLogger->info("User {$currentUser->user_name} left organisation {$organisation->name}.", [
|
||||
|
||||
@@ -75,7 +75,7 @@ class OrganisationRegistrationController extends SimpleController
|
||||
throw new ForbiddenException();
|
||||
}
|
||||
|
||||
if ($config['organisation']['membership']['single_membership'] && $currentUser->organisations()->count() > 0) {
|
||||
if ($config['organisation']['membership']['single_membership'] && $currentUser->organisations(true)->count() > 0) {
|
||||
$ms->addMessageTranslated('danger', 'ORGANISATION.REGISTRATION.ALREADY_IN_ONE', $data);
|
||||
return $response->withJson([], 400);
|
||||
}
|
||||
@@ -600,7 +600,7 @@ class OrganisationRegistrationController extends SimpleController
|
||||
throw new ForbiddenException();
|
||||
}
|
||||
|
||||
if ($config['organisation']['membership']['single_membership'] && $currentUser->organisations()->count() > 0) {
|
||||
if ($config['organisation']['membership']['single_membership'] && $currentUser->organisations(true)->count() > 0) {
|
||||
throw new BadRequestException();
|
||||
}
|
||||
|
||||
|
||||
@@ -255,7 +255,7 @@ class UserController extends UFUserController
|
||||
foreach ($fieldValue as $field) {
|
||||
$newOrganisations[$field['organisation_id']] = ['flag_admin' => $field['flag_admin'] == 1];
|
||||
}
|
||||
$user->organisations()->sync($newOrganisations);
|
||||
$user->organisations(true)->sync($newOrganisations);
|
||||
} else {
|
||||
$user->$fieldName = $fieldValue;
|
||||
$user->save();
|
||||
|
||||
@@ -32,7 +32,30 @@ class User extends UFUser
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
||||
*/
|
||||
public function organisations()
|
||||
public function organisations($withPending = false)
|
||||
{
|
||||
/** @var \UserFrosting\Sprinkle\Core\Util\ClassMapper $classMapper */
|
||||
$classMapper = static::$ci->classMapper;
|
||||
|
||||
$query = $this->belongsToMany(
|
||||
$classMapper->getClassMapping('organisation'), 'organisation_members', 'user_id', 'organisation_id'
|
||||
)
|
||||
->orderBy('organisations.name', 'asc')
|
||||
->withPivot(['flag_admin', 'flag_approved']);
|
||||
|
||||
if ($withPending !== true) {
|
||||
$query = $query->wherePivot('flag_approved', true);
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all organisations this user is attempting to join.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
||||
*/
|
||||
public function pendingOrganisations()
|
||||
{
|
||||
/** @var \UserFrosting\Sprinkle\Core\Util\ClassMapper $classMapper */
|
||||
$classMapper = static::$ci->classMapper;
|
||||
@@ -41,7 +64,8 @@ class User extends UFUser
|
||||
$classMapper->getClassMapping('organisation'), 'organisation_members', 'user_id', 'organisation_id'
|
||||
)
|
||||
->orderBy('organisations.name', 'asc')
|
||||
->withPivot(['flag_admin', 'flag_approved']);
|
||||
->withPivot(['flag_admin', 'flag_approved'])
|
||||
->wherePivot('flag_approved', false);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,7 +89,7 @@ class User extends UFUser
|
||||
->where('approver_id', $this->id)
|
||||
->update(['approver_id' => null]);
|
||||
|
||||
$this->organisations()->detach();
|
||||
$this->organisations(true)->detach();
|
||||
$this->refresh();
|
||||
}
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ class UserSprunje extends UFUserSprunje
|
||||
$query = parent::baseQuery();
|
||||
|
||||
// Join user's organisations
|
||||
return $query->joinOrganisations()->with('organisations');
|
||||
return $query->joinOrganisations(true)->with('organisations')->with('pendingOrganisations');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user