Skip to content

Commit

Permalink
Merge pull request #175 from OpenSynergic/imp/submission-tabs
Browse files Browse the repository at this point in the history
Fix: Submission List based on user role
  • Loading branch information
rahmanramsi authored Dec 30, 2023
2 parents 37732b1 + 5950bdf commit bbd5ea5
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 395 deletions.
10 changes: 5 additions & 5 deletions app/Models/Submission.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ protected static function booted(): void
]);

//If current user does not exists in participant
if (! $userAsParticipant = $submission->user->asParticipant()) {
if (!$userAsParticipant = $submission->user->asParticipant()) {
$userAsParticipant = CreateParticipantFromUserAction::run($submission->user);
}

Expand Down Expand Up @@ -155,14 +155,14 @@ public function contributors()
return $this->hasMany(SubmissionContributor::class);
}

public function scopeStage(Builder $query, SubmissionStage $stage)
public function scopePublished(Builder $query)
{
return $query->where('stage', $stage);
return $query->status(SubmissionStatus::Published);
}

public function scopePublished(Builder $query)
public function scopeStage(Builder $query, SubmissionStage $stage)
{
return $query->status(SubmissionStatus::Published);
return $query->where('stage', $stage);
}

public function scopeStatus(Builder $query, SubmissionStatus $status)
Expand Down
8 changes: 8 additions & 0 deletions app/Models/SubmissionParticipant.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Models;

use App\Models\Enums\UserRole;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

Expand All @@ -27,6 +29,12 @@ public function role()
return $this->belongsTo(Role::class);
}

public function scopeEditor(Builder $builder)
{
$roleEditor = Role::where('name', UserRole::Editor->value)->first();
return $builder->where('role_id', $roleEditor->getKey());
}

public function submission()
{
return $this->belongsTo(Submission::class);
Expand Down
21 changes: 19 additions & 2 deletions app/Panel/Livewire/Submissions/Components/ParticipantList.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,24 @@ function (Model $record) {
->toArray()
)
->searchable()
->preload()
->getSearchResultsUsing(function (Get $get, string $search) {
return User::with('roles')
->whereHas(
'roles',
fn (Builder $query) => $query->whereId($get('role_id'))
)
->whereNotIn('id', $this->submission->participants->pluck('user_id'))
->where('given_name', 'like', "%{$search}%")
->orWhere('family_name', 'like', "%{$search}%")
->orWhere('email', 'like', "%{$search}%")
->get()
->mapWithKeys(
fn (User $user) => [
$user->getKey() => static::renderSelectParticipant($user),
]
)
->toArray();
})
->columnSpan(2),
Fieldset::make()
->label('Notification')
Expand Down Expand Up @@ -262,7 +279,7 @@ function (Message $message) use ($data) {
->color('primary')
->redirectTo('panel')
->action(function (SubmissionParticipant $record, Impersonate $action) {
if (! $action->impersonate($record->user)) {
if (!$action->impersonate($record->user)) {
$action->failureNotificationTitle("User can't be impersonated");
$action->failure();
}
Expand Down
181 changes: 108 additions & 73 deletions app/Panel/Resources/SubmissionResource/Pages/ManageSubmissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,21 @@ class ManageSubmissions extends ManageRecords

protected static string $view = 'panel.resources.submission-resource.pages.list-submission';

protected const TAB_MYQUEUE = 'My Queue';

protected const TAB_ACTIVE = 'Active';

protected const TAB_PUBLISHED = 'Published';

protected const TAB_ARCHIVED = 'Archived';

public function infolist(Infolist $infolist): Infolist
{
return $infolist
->schema([
ShoutEntry::make('title')
->hidden(function () {
return StageManager::callForAbstract()->isStageOpen() || ! Auth::user()->can('Workflow:update');
return StageManager::callForAbstract()->isStageOpen() || !Auth::user()->can('Workflow:update');
})
->type('warning')
->content(function () {
Expand All @@ -53,7 +61,7 @@ protected function getHeaderActions(): array
->button()
->authorize('Submission:create')
->disabled(
fn (): bool => ! StageManager::callForAbstract()->isStageOpen()
fn (): bool => !StageManager::callForAbstract()->isStageOpen()
)
->url(static::$resource::getUrl('create'))
->icon('heroicon-o-plus')
Expand All @@ -67,82 +75,109 @@ protected function getHeaderActions(): array
];
}

public function getTabs(): array
protected static function generateQueryByCurrentUser(string $tabs)
{
return [
'My Queue' => Tab::make('My Queue')
->when(auth()->user()->hasRole(UserRole::Author->value), function (Tab $tab) {
return $tab->modifyQueryUsing(function (Builder $query) {
return $query->where('user_id', auth()->id());
});
})
->when(auth()->user()->hasRole(UserRole::Reviewer->value), function (Tab $tab) {
return $tab->modifyQueryUsing(function (Builder $query) {
return $query->whereHas(
'reviews',
fn (Builder $query) => $query->where('user_id', auth()->id())
);
});
})
->when(auth()->user()->hasRole(UserRole::Editor->value), function (Tab $tab) {
return $tab->modifyQueryUsing(function (Builder $query) {
return $query->whereHas(
'participants',
fn (Builder $query) => $query->where('user_id', auth()->id())
);
});
}),
'Active' => Tab::make('Active')
->when(
auth()->user()->hasRole(UserRole::Author->value),
function (Tab $tab) {
return $tab->modifyQueryUsing(function (Builder $query) {
return $query->where('user_id', auth()->id());
$statuses = match ($tabs) {
static::TAB_MYQUEUE => [
SubmissionStatus::Queued,
],
static::TAB_ACTIVE => [
SubmissionStatus::OnReview,
SubmissionStatus::Editing,
],
static::TAB_PUBLISHED => [
SubmissionStatus::Published,
],
static::TAB_ARCHIVED => [
SubmissionStatus::Declined,
SubmissionStatus::Withdrawn,
],
default => null,
};

$query = static::getResource()::getEloquentQuery();

if (Auth::user()->hasAnyRole([
UserRole::Admin->value,
UserRole::ConferenceManager->value
])) {
return $query->whereIn('status', $statuses)->when(
$tabs == static::TAB_MYQUEUE,
function (Builder $query) {
$query->orWhere([
['user_id', '=', Auth::id()],
['status', '=', SubmissionStatus::Incomplete]
]);
}
);
}


// Digunakan untuk menentukan mengetahui kondisi sebelumnya sudah ada atau belum
$conditionBeforeExist = false;
return $query->when(
Auth::user()->hasRole(UserRole::Author->value),
function (Builder $query) use ($statuses, &$conditionBeforeExist) {
$query->where('user_id', Auth::id())->whereIn('status', $statuses);
$conditionBeforeExist = true;
}
)->when(
Auth::user()->hasRole(UserRole::Reviewer->value),
function (Builder $query) use (&$conditionBeforeExist, $tabs, $statuses) {
$query->when(
$conditionBeforeExist,
function (Builder $query) {
$query->orWhereHas('reviews', function (Builder $query) {
return $query->where('user_id', Auth::id());
});
},
function (Builder $query) {
$query->whereHas('reviews', function (Builder $query) {
return $query->where('user_id', Auth::id());
});
}
)
->when(auth()->user()->hasRole(UserRole::Reviewer->value), function (Tab $tab) {
return $tab->modifyQueryUsing(function (Builder $query) {
return $query->whereHas(
'reviews',
fn (Builder $query) => $query->where('user_id', auth()->id())
);
});
}),
'Published' => Tab::make('Published')
->when(auth()->user()->hasRole(UserRole::Author->value), function (Tab $tab) {
return $tab->modifyQueryUsing(function (Builder $query) {
return $query->where('user_id', auth()->id());
});
})
->when(auth()->user()->hasRole(UserRole::Reviewer->value), function (Tab $tab) {
return $tab->modifyQueryUsing(function (Builder $query) {
return $query->whereHas(
'reviews',
fn (Builder $query) => $query->where('user_id', auth()->id())
);
});
})
->modifyQueryUsing(function (Builder $query) {
return $query->where('status', SubmissionStatus::Published);
}),
'Declined' => Tab::make('Declined')
->when(auth()->user()->hasRole(UserRole::Author->value), function (Tab $tab) {
return $tab->modifyQueryUsing(function (Builder $query) {
return $query->where('user_id', auth()->id());
)->when($tabs != static::TAB_MYQUEUE, function (Builder $query) use ($statuses) {
$query->whereIn('status', $statuses);
});
$conditionBeforeExist = true;
}
)->when(
Auth::user()->hasRole(UserRole::Editor->value),
function (Builder $query) use ($statuses, &$conditionBeforeExist) {
$query->when($conditionBeforeExist, function (Builder $query) {
$query->orWhereHas('participants', function (Builder $query) {
return $query->where('user_id', Auth::id());
});
})
->when(auth()->user()->hasRole(UserRole::Reviewer->value), function (Tab $tab) {
return $tab->modifyQueryUsing(function (Builder $query) {
return $query->whereHas(
'reviews',
fn (Builder $query) => $query->where('user_id', auth()->id())
);
}, function (Builder $query) {
$query->whereHas('participants', function (Builder $query) {
return $query->where('user_id', Auth::id());
});
})
->modifyQueryUsing(function (Builder $query) {
return $query->where('status', SubmissionStatus::Declined);
}),
})->whereIn('status', $statuses);
}
)->when(
$tabs == static::TAB_MYQUEUE,
function (Builder $query) {
$query->orWhere([
['user_id', '=', Auth::id()],
['status', '=', SubmissionStatus::Incomplete]
]);
}
);
}

/** Need to be optimized */
public function getTabs(): array
{

return [
static::TAB_MYQUEUE => Tab::make("My Queue")
->modifyQueryUsing(fn (): Builder => static::generateQueryByCurrentUser(static::TAB_MYQUEUE)),
static::TAB_ACTIVE => Tab::make("Active")
->modifyQueryUsing(fn (): Builder => static::generateQueryByCurrentUser(static::TAB_ACTIVE)),
static::TAB_PUBLISHED => Tab::make("Published")
->modifyQueryUsing(fn (): Builder => static::generateQueryByCurrentUser(static::TAB_PUBLISHED)),
static::TAB_ARCHIVED => Tab::make("Archived")
->modifyQueryUsing(fn (): Builder => static::generateQueryByCurrentUser(static::TAB_ARCHIVED)),
];
}
}
8 changes: 6 additions & 2 deletions app/Policies/SubmissionPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ public function viewAny(User $user)
return $user->can('Submission:viewAny');
}

public function view(User $user)
public function view(User $user, Submission $submission)
{
if ($submission->participants()->where('user_id', $user->getKey())->exists()) {
return true;
}

if ($user->can('Submission:view')) {
return true;
}
Expand Down Expand Up @@ -285,7 +289,7 @@ public function withdraw(User $user, Submission $submission)
}

// Editors cannot withdraw submissions; they must wait for the author to request it..
if (! filled($submission->withdrawn_reason)) {
if (!filled($submission->withdrawn_reason)) {
return false;
}

Expand Down
Loading

0 comments on commit bbd5ea5

Please sign in to comment.