diff --git a/app/Http/Controllers/v1/Asso/AccessController.php b/app/Http/Controllers/v1/Asso/AccessController.php index 33bf01287..e09f02072 100644 --- a/app/Http/Controllers/v1/Asso/AccessController.php +++ b/app/Http/Controllers/v1/Asso/AccessController.php @@ -6,6 +6,7 @@ * TODO: Missing Scopes ! * * @author Samy Nastuzzi + * @author Noé Amiot * * @copyright Copyright (c) 2018, SiMDE-UTC * @license GNU GPL-3.0 @@ -105,7 +106,7 @@ protected function getAccess(Request $request, string $access_id, string $user_i */ public function index(Request $request, string $asso_id): JsonResponse { - $choices = $this->getChoices($request); + $choices = $this->getChoices($request, ["joined", "joining"], ["joined"]); $semester = $this->getSemester($request, $choices); $asso = $this->getAsso($request, $asso_id, \Auth::user(), $semester); $access = $asso->access()->where('semester_id', $semester->id); @@ -116,6 +117,12 @@ public function index(Request $request, string $asso_id): JsonResponse }); } + if (in_array('joining', $choices) && count($choices) <= 1) { + $access = $access->whereNull('validated_by_id'); + } else if (in_array('joined', $choices) && count($choices) <= 1) { + $access = $access->whereNotNull('validated_by_id'); + } + $access = $access->getSelection() ->map(function ($access) { return $access->hideData(); @@ -179,13 +186,20 @@ public function store(AccessRequest $request, string $asso_id): JsonResponse */ public function show(Request $request, string $asso_id, string $access_id): JsonResponse { - $choices = $this->getChoices($request); + $choices = $this->getChoices($request, ["joined", "joining"], ["joined"]); $semester = $this->getSemester($request, $choices); $user_id = (\Auth::id() ?? $request->input('user_id')); $asso = $this->getAsso($request, $asso_id, \Auth::user(), $semester); $access = $this->getAccess($request, $access_id, $user_id, $asso, $semester->id); + $validated = (isset($access["validated"]) && $access["validated"]); + + if (in_array('joined', $choices) && $validated) { + return response()->json($access->hideSubData(), 200); + } else if (in_array('joining', $choices) && !$validated) { + return response()->json($access->hideSubData(), 200); + } - return response()->json($access->hideSubData(), 200); + return response()->json([], 200); } /** diff --git a/app/Http/Controllers/v1/Asso/MemberController.php b/app/Http/Controllers/v1/Asso/MemberController.php index 910d8bfeb..d6b766f03 100644 --- a/app/Http/Controllers/v1/Asso/MemberController.php +++ b/app/Http/Controllers/v1/Asso/MemberController.php @@ -4,6 +4,7 @@ * * @author Samy Nastuzzi * @author Rémy Huet + * @author Noé Amiot * * @copyright Copyright (c) 2018, SiMDE-UTC * @license GNU GPL-3.0 @@ -121,23 +122,56 @@ protected function addRolesAndPermissions(Asso $asso, User $member) */ public function index(Request $request, string $asso_id): JsonResponse { - $choices = $this->getChoices($request); + $choices = $this->getChoices($request, ["joined", "joining", "followed"], ["joined"]); $semester = $this->getSemester($request, $choices); $asso = $this->getAsso($request, $asso_id); - $members = $asso->allMembers() - ->where('semester_id', $semester->id) - ->whereNotNull('role_id') - ->getSelection(true) - ->map(function ($member) { - $member->pivot = [ - 'role_id' => $member->role_id, - 'validated_by_id' => $member->validated_by_id, - 'semester_id' => $member->semester_id, - ]; + $members = collect(); - return $member->hideData(); - }); + // By default, we show all joined. + if (in_array('joined', $choices)) { + $members = $members->merge($asso->members() + ->where('semester_id', $semester->id) + ->whereNotNull('role_id') + ->getSelection(true) + ->map(function ($member) { + $member->pivot = [ + 'role_id' => $member->role_id, + 'validated_by_id' => $member->validated_by_id, + 'semester_id' => $member->semester_id, + ]; + return $member->hideData(); + })); + } + + if (in_array('joining', $choices)) { + $members = $members->merge($asso->joiners() + ->where('semester_id', $semester->id) + ->whereNotNull('role_id') + ->getSelection(true) + ->map(function ($member) { + $member->pivot = [ + 'role_id' => $member->role_id, + 'validated_by_id' => $member->validated_by_id, + 'semester_id' => $member->semester_id, + ]; + return $member->hideData(); + })); + } + + if (in_array('followed', $choices)) { + $members = $members->merge($asso->followers() + ->where('semester_id', $semester->id) + ->getSelection(true) + ->map(function ($member) { + $member->pivot = [ + 'role_id' => $member->role_id, + 'validated_by_id' => $member->validated_by_id, + 'semester_id' => $member->semester_id, + ]; + return $member->hideData(); + })); + } return response()->json($members, 200); } @@ -177,12 +211,24 @@ public function store(AssoMemberRequest $request, string $asso_id): JsonResponse */ public function show(Request $request, string $asso_id, string $member_id): JsonResponse { - $choices = $this->getChoices($request); + $choices = $this->getChoices($request, ["joined", "joining", "followed"], ["joined"]); $semester = $this->getSemester($request, $choices); $asso = $this->getAsso($request, $asso_id); $user = $this->getUserFromAsso($request, $asso, $member_id, $semester); - return response()->json($user->hideData(), 200); + if (in_array('joined', $choices) + && !is_null($user["pivot"]["validated_by_id"]) + && !is_null($user["pivot"]["role_id"])) { + return response()->json($user->hideData(), 200); + } else if (in_array('joining', $choices) + && is_null($user["pivot"]["validated_by_id"]) + && !is_null($user["pivot"]["role_id"])) { + return response()->json($user->hideData(), 200); + } else if (in_array('followed', $choices) && $user["pivot"]["role_id"] == null) { + return response()->json($user->hideData(), 200); + } + + return response()->json([], 200); } /** diff --git a/app/Http/Controllers/v1/Controller.php b/app/Http/Controllers/v1/Controller.php index 815e3d2d5..a752e4b84 100644 --- a/app/Http/Controllers/v1/Controller.php +++ b/app/Http/Controllers/v1/Controller.php @@ -3,6 +3,7 @@ * V1 controller. * * @author Samy Nastuzzi + * @author Noé Amiot * * @copyright Copyright (c) 2018, SiMDE-UTC * @license GNU GPL-3.0 @@ -32,10 +33,11 @@ class Controller extends BaseController * * @param Request $request * @param array $choices + * @param array $defaultChoices * @return array * @throws PortailException For bad $choices. */ - protected function getChoices(Request $request, array $choices) + protected function getChoices(Request $request, array $choices, array $defaultChoices=[]) { $only = $request->input('only') ? explode(',', $request->input('only')) : []; $except = $request->input('except') ? explode(',', $request->input('except')) : []; @@ -45,6 +47,10 @@ protected function getChoices(Request $request, array $choices) throw new PortailException('Il n\'est possible de spécifier uniquement: '.implode(', ', $choices)); } + if (empty($only) && empty($except)) { + return $defaultChoices; + } + if (count($only) > 0) { $choices = $only; } diff --git a/app/Http/Controllers/v1/User/AssoController.php b/app/Http/Controllers/v1/User/AssoController.php index c6b1d2a16..6070b4802 100644 --- a/app/Http/Controllers/v1/User/AssoController.php +++ b/app/Http/Controllers/v1/User/AssoController.php @@ -65,7 +65,7 @@ public function __construct() public function index(Request $request, string $user_id=null): JsonResponse { $user = $this->getUser($request, $user_id); - $choices = $this->getChoices($request, ['joined', 'joining', 'followed']); + $choices = $this->getChoices($request, ['joined', 'joining', 'followed'], ["joined"]); $semester = $this->getSemester($request, $choices); $assos = collect(); diff --git a/app/Traits/Controller/v1/HasAssos.php b/app/Traits/Controller/v1/HasAssos.php index 18b65f648..cce4e61a9 100644 --- a/app/Traits/Controller/v1/HasAssos.php +++ b/app/Traits/Controller/v1/HasAssos.php @@ -3,6 +3,7 @@ * Add the controller an access to Associations. * * @author Samy Nastuzzi + * @author Noé Amiot * * @copyright Copyright (c) 2018, SiMDE-UTC * @license GNU GPL-3.0 @@ -66,12 +67,14 @@ protected function getSemester(Request $request, array $choices, string $verb='g * * @param Request $request * @param array $initialChoices + * @param array $defaultChoices * @return array */ - protected function getChoices(Request $request, array $initialChoices=['joined', 'joining']) + protected function getChoices(Request $request, array $initialChoices=['joined', 'joining'], array $defaultChoices=[]) { $scopeHead = \Scopes::getTokenType($request); $choices = []; + $askedDefaultChoices = []; foreach ($initialChoices as $choice) { if (\Scopes::hasOne($request, $scopeHead.'-get-assos-members-'.$choice.'-now')) { @@ -79,7 +82,15 @@ protected function getChoices(Request $request, array $initialChoices=['joined', } } - return parent::getChoices($request, $choices); + foreach ($defaultChoices as $choice) { + if (!in_array($choice, $choices)) { + throw new PortailException('Les choix demandés ne peuvent qu\'appartienir à: '.implode(', ', $choices)); + } + + $askedDefaultChoices[] = $choice; + } + + return parent::getChoices($request, $choices, $askedDefaultChoices); } /** @@ -127,7 +138,6 @@ protected function getUserFromAsso(Request $request, Asso $asso, string $user_id $user = $asso->allMembers() ->wherePivot('user_id', $this->getUser($request, $user_id, true)->id) ->wherePivot('semester_id', $semester ? $semester->id : Semester::getThisSemester()) - ->whereNotNull('role_id') ->first(); if ($user) { diff --git a/app/Traits/Model/HasMembers.php b/app/Traits/Model/HasMembers.php index 33847dcb3..6fda193f0 100644 --- a/app/Traits/Model/HasMembers.php +++ b/app/Traits/Model/HasMembers.php @@ -82,8 +82,7 @@ public function currentAllMembers() public function members() { return $this->belongsToMany(User::class, $this->getMemberRelationTable()) - ->whereNotNull('validated_by_id') - ->withPivot('semester_id', 'role_id', 'validated_by_id', 'created_at', 'updated_at'); + ->withPivot('semester_id', 'role_id', 'validated_by_id', 'created_at', 'updated_at'); } /** diff --git a/resources/assets/react/AppLoader.js b/resources/assets/react/AppLoader.js index d8458134a..87a646295 100644 --- a/resources/assets/react/AppLoader.js +++ b/resources/assets/react/AppLoader.js @@ -53,7 +53,7 @@ class AppLoader extends React.Component { // User permissions retrievement. dispatch(actions.user.permissions.all()); // User associations retrievement. - dispatch(actions.user.assos.all()); + dispatch(actions.user.assos.all({ only: 'joined,joining,followed' })); // User services retrievement. dispatch(actions.user.services.all()); diff --git a/resources/assets/react/screens/Asso/Access.js b/resources/assets/react/screens/Asso/Access.js index 68e8cce63..4453f3b74 100644 --- a/resources/assets/react/screens/Asso/Access.js +++ b/resources/assets/react/screens/Asso/Access.js @@ -2,6 +2,7 @@ * Display the access demands of an association. * * @author Samy Nastuzzi + * @author Noé Amiot * * @copyright Copyright (c) 2019, SiMDE-UTC * @license GNU GPL-3.0 @@ -61,7 +62,7 @@ class AccessScreen extends React.Component { } if (!accessFetched) { - dispatch(actions.access.all()); + dispatch(actions.access.all({ only: 'joined,joining' })); } dispatch(actions.config({ title: `${shortname} - Accès` })); @@ -70,7 +71,7 @@ class AccessScreen extends React.Component { loadAssosData(id) { const { dispatch } = this.props; - dispatch(actions.assos(id).access.all()); + dispatch(actions.assos(id).access.all({ only: 'joined,joining' })); } sendDemand(data) { @@ -80,7 +81,7 @@ class AccessScreen extends React.Component { .assos(asso.id) .access.create(data) .payload.then(({ data: { id: access_id } }) => { - dispatch(actions.assos(asso.id).access.all()); + dispatch(actions.assos(asso.id).access.all({ only: 'joined,joining' })); NotificationManager.success( "La demande d'accès a été envoyée. En attente de la confirmation d'un responsable de l'association", "Demande d'accès" @@ -95,7 +96,7 @@ class AccessScreen extends React.Component { .access(access_id) .update() .payload.then(() => { - dispatch(actions.assos(asso.id).access.all()); + dispatch(actions.assos(asso.id).access.all({ only: 'joined,joining' })); NotificationManager.success( "La demande d'accès a été automatiquement confirmée. En attente de validation de l'accès", "Demande d'accès" @@ -127,7 +128,7 @@ class AccessScreen extends React.Component { .access(acces.id) .update() .payload.then(() => { - dispatch(actions.assos(asso.id).access.all()); + dispatch(actions.assos(asso.id).access.all({ only: 'joined,joining' })); NotificationManager.success( "La demande d'accès a été confirmée. En attente de validation de l'accès", "Demande d'accès" @@ -149,7 +150,7 @@ class AccessScreen extends React.Component { .access(acces.id) .delete() .payload.then(() => { - dispatch(actions.assos(asso.id).access.all()); + dispatch(actions.assos(asso.id).access.all({ only: 'joined,joining' })); NotificationManager.success("La demande d'accès a été annulée", "Demande d'accès"); }) .catch(() => { diff --git a/resources/assets/react/screens/Asso/index.js b/resources/assets/react/screens/Asso/index.js index c13effeb3..f661b8155 100644 --- a/resources/assets/react/screens/Asso/index.js +++ b/resources/assets/react/screens/Asso/index.js @@ -157,7 +157,7 @@ class AssoScreen extends React.Component { } ) .payload.then(() => { - dispatch(actions.user.assos.all()); + dispatch(actions.user.assos.all({ only: 'joined,joining,followed' })); dispatch(actions.assos(asso.id).members.all()); NotificationManager.success( `Vous suivez maintenant l'association: ${asso.name}`, @@ -201,7 +201,7 @@ class AssoScreen extends React.Component { actions.user.assos .remove(asso.id) .payload.then(() => { - dispatch(actions.user.assos.all()); + dispatch(actions.user.assos.all({ only: 'joined,joining,followed' })); dispatch(actions.assos(asso.id).members.all()); NotificationManager.warning( `Vous ne suivez plus l'association: ${asso.name}`, @@ -283,7 +283,7 @@ class AssoScreen extends React.Component { .payload.then(({ data: { id: member_id } }) => { const { user, asso } = this.props; - dispatch(actions.user.assos.all()); + dispatch(actions.user.assos.all({ only: 'joined,joining,followed' })); dispatch(actions.assos(asso.id).members.all()); NotificationManager.success( `Vous avez demandé à rejoindre l'association: ${asso.name}`, @@ -301,7 +301,7 @@ class AssoScreen extends React.Component { ); if (user.id === member_id) { - dispatch(actions.user.assos.all()); + dispatch(actions.user.assos.all({ only: 'joined,joining,followed' })); dispatch(actions.user.permissions.all()); } }) @@ -357,7 +357,7 @@ class AssoScreen extends React.Component { .assos(asso.id) .members.remove(user.id) .payload.then(() => { - dispatch(actions.user.assos.all()); + dispatch(actions.user.assos.all({ only: 'joined,joining,followed' })); dispatch(actions.assos(asso.id).members.all()); NotificationManager.warning( `Vous ne faites plus partie de l'association: ${name}`, @@ -414,7 +414,7 @@ class AssoScreen extends React.Component { ); if (user.id === member_id) { - dispatch(actions.user.assos.all()); + dispatch(actions.user.assos.all({ only: 'joined,joining,followed' })); dispatch(actions.user.permissions.all()); } }) @@ -459,7 +459,7 @@ class AssoScreen extends React.Component { .assos(asso.id) .members.remove(member_id) .payload.then(() => { - dispatch(actions.user.assos.all()); + dispatch(actions.user.assos.all({ only: 'joined,joining,followed' })); dispatch(actions.assos(asso.id).members.all()); NotificationManager.warning( `Vous avez retiré avec succès le membre de cette association: ${name}`,