Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/api returns unvalidated relations #345

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
20 changes: 17 additions & 3 deletions app/Http/Controllers/v1/Asso/AccessController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* TODO: Missing Scopes !
*
* @author Samy Nastuzzi <[email protected]>
* @author Noé Amiot <[email protected]>
*
* @copyright Copyright (c) 2018, SiMDE-UTC
* @license GNU GPL-3.0
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}

/**
Expand Down
76 changes: 61 additions & 15 deletions app/Http/Controllers/v1/Asso/MemberController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*
* @author Samy Nastuzzi <[email protected]>
* @author Rémy Huet <[email protected]>
* @author Noé Amiot <[email protected]>
*
* @copyright Copyright (c) 2018, SiMDE-UTC
* @license GNU GPL-3.0
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}

/**
Expand Down
8 changes: 7 additions & 1 deletion app/Http/Controllers/v1/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* V1 controller.
*
* @author Samy Nastuzzi <[email protected]>
* @author Noé Amiot <[email protected]>
*
* @copyright Copyright (c) 2018, SiMDE-UTC
* @license GNU GPL-3.0
Expand Down Expand Up @@ -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')) : [];
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/v1/User/AssoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
16 changes: 13 additions & 3 deletions app/Traits/Controller/v1/HasAssos.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Add the controller an access to Associations.
*
* @author Samy Nastuzzi <[email protected]>
* @author Noé Amiot <[email protected]>
*
* @copyright Copyright (c) 2018, SiMDE-UTC
* @license GNU GPL-3.0
Expand Down Expand Up @@ -66,20 +67,30 @@ 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')) {
$choices[] = $choice;
}
}

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

/**
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions app/Traits/Model/HasMembers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion resources/assets/react/AppLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
13 changes: 7 additions & 6 deletions resources/assets/react/screens/Asso/Access.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Display the access demands of an association.
*
* @author Samy Nastuzzi <[email protected]>
* @author Noé Amiot <[email protected]>
*
* @copyright Copyright (c) 2019, SiMDE-UTC
* @license GNU GPL-3.0
Expand Down Expand Up @@ -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` }));
Expand All @@ -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) {
Expand All @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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(() => {
Expand Down
14 changes: 7 additions & 7 deletions resources/assets/react/screens/Asso/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down Expand Up @@ -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}`,
Expand Down Expand Up @@ -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}`,
Expand All @@ -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());
}
})
Expand Down Expand Up @@ -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}`,
Expand Down Expand Up @@ -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());
}
})
Expand Down Expand Up @@ -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}`,
Expand Down