Skip to content

Commit

Permalink
IP-302: The content manager should only see their created content (pr…
Browse files Browse the repository at this point in the history
…ojects/questionnaires/problems etc)
  • Loading branch information
PavlosIsaris committed Nov 25, 2024
1 parent 51c2d42 commit d129076
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ public function __construct(CrowdSourcingProjectRepository $crowdSourcingProject

public function getProjectsUserHasAccessToEdit(User $user): Collection {
$relationships = ['creator', 'language', 'status'];
if ($this->userRoleManager->userHasAdminRole($user) || $this->userRoleManager->userHasContentManagerRole($user)) {
if ($this->userRoleManager->userHasAdminRole($user)) {
return $this->crowdSourcingProjectRepository
->allWithTrashed(['*'], 'id', 'desc', $relationships);
}

// if the user is content manager, return only the projects created by them
return $this->crowdSourcingProjectRepository->whereWithTrashed(['user_creator_id' => $user->id], ['*'],
'id', 'desc', $relationships);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\BusinessLogicLayer\lkp\CrowdSourcingProjectStatusLkp;
use App\BusinessLogicLayer\Questionnaire\QuestionnaireGoalManager;
use App\BusinessLogicLayer\User\UserManager;
use App\BusinessLogicLayer\User\UserRoleManager;
use App\Models\CrowdSourcingProject\CrowdSourcingProject;
use App\Notifications\QuestionnaireResponded;
use App\Repository\CrowdSourcingProject\CrowdSourcingProjectRepository;
Expand Down Expand Up @@ -43,6 +44,7 @@ class CrowdSourcingProjectManager {
protected QuestionnaireResponseRepository $questionnaireResponseRepository;
protected CrowdSourcingProjectTranslationManager $crowdSourcingProjectTranslationManager;
protected ProblemRepository $crowdSourcingProjectProblemRepository;
protected UserRoleManager $userRoleManager;

public function __construct(CrowdSourcingProjectRepository $crowdSourcingProjectRepository,
QuestionnaireRepository $questionnaireRepository,
Expand All @@ -54,7 +56,8 @@ public function __construct(CrowdSourcingProjectRepository $crowdSourcingProject
CrowdSourcingProjectColorsManager $crowdSourcingProjectColorsManager,
QuestionnaireResponseRepository $questionnaireResponseRepository,
CrowdSourcingProjectTranslationManager $crowdSourcingProjectTranslationManager,
ProblemRepository $crowdSourcingProjectProblemRepository) {
ProblemRepository $crowdSourcingProjectProblemRepository,
UserRoleManager $userRoleManager) {
$this->crowdSourcingProjectRepository = $crowdSourcingProjectRepository;
$this->questionnaireRepository = $questionnaireRepository;
$this->crowdSourcingProjectStatusManager = $crowdSourcingProjectStatusManager;
Expand All @@ -66,6 +69,7 @@ public function __construct(CrowdSourcingProjectRepository $crowdSourcingProject
$this->questionnaireResponseRepository = $questionnaireResponseRepository;
$this->crowdSourcingProjectTranslationManager = $crowdSourcingProjectTranslationManager;
$this->crowdSourcingProjectProblemRepository = $crowdSourcingProjectProblemRepository;
$this->userRoleManager = $userRoleManager;
}

public function getCrowdSourcingProjectsForHomePage(): Collection {
Expand Down Expand Up @@ -465,7 +469,9 @@ public function getAllCrowdSourcingProjectsWithDefaultTranslation(): Collection
}

public function getCrowdSourcingProjectsForProblems(): Collection {
// get all projects that have at least one problem
return $this->crowdSourcingProjectRepository->getProjectsForProblems();
$user = Auth::user();
$user_cretor_id = $this->userRoleManager->userHasAdminRole($user) ? null : $user->id;

return $this->crowdSourcingProjectRepository->getProjectsForProblems($user_cretor_id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,15 @@ public function getAllProjectsWithDefaultTranslation($additionalRelationships =
return $builder->get();
}

public function getProjectsForProblems(): Collection {
// get all projects that are not draft, not unpublished, and have at least one problem
return CrowdSourcingProject::where('status_id', '!=', CrowdSourcingProjectStatusLkp::DRAFT)
public function getProjectsForProblems(?int $user_creator_id): Collection {
$builder = CrowdSourcingProject::where('status_id', '!=', CrowdSourcingProjectStatusLkp::DRAFT)
->where('status_id', '!=', CrowdSourcingProjectStatusLkp::UNPUBLISHED)
->whereHas('problems')
->get();
->whereHas('problems');

if (!is_null($user_creator_id)) {
$builder->where('user_creator_id', $user_creator_id);
}

return $builder->get();
}
}
2 changes: 1 addition & 1 deletion database/seeders/DefaultProjectSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function run() {
'external_url' => null,
'img_path' => '/images/projects/european-democracy/logo-bg.webp',
'logo_path' => '/images/projects/european-democracy/logo.webp',
'user_creator_id' => 1,
'user_creator_id' => 2,
'language_id' => 6,
'should_send_email_after_questionnaire_response' => 1,
'lp_primary_color' => '#0069d9',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<select
id="projectSelect"
:class="['form-select form-control mt-3', projectsFetched ? '' : 'hidden']"
v-model="selectedProject"
v-model="selectedProjectId"
@change="getProjectProblems"
>
<option value="" disabled selected>Select a Project</option>
Expand Down Expand Up @@ -166,7 +166,7 @@ export default {
return {
fetched: false,
projects: [],
selectedProject: "",
selectedProjectId: "",
problems: [],
problemStatuses: [],
errorMessage: "",
Expand Down Expand Up @@ -259,19 +259,24 @@ export default {
.then((response) => {
this.projects = response.data;
this.projectsFetched = true;
// if only one project is available, select it by default and fetch its problems
if (this.projects.length === 1) {
this.selectedProjectId = this.projects[0].id;
this.getProjectProblems();
}
})
.catch((error) => {
this.showErrorMessage(error);
});
},
getProjectProblems() {
if (this.selectedProject) {
if (this.selectedProjectId) {
this.fetched = false;
this.problems = [];
this.post({
url: window.route("api.problems.get-management"),
data: { projectId: this.selectedProject },
data: { projectId: this.selectedProjectId },
urlRelative: false,
})
.then((response) => {
Expand Down

0 comments on commit d129076

Please sign in to comment.