You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
5.0 KiB

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Project;
  4. use App\Business;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\DB;
  7. class ProjectController extends Controller
  8. {
  9. public function index(Request $request, int $business)
  10. {
  11. // permit('businessAccess');
  12. return Project::where('business_id', $business)->get();
  13. }
  14. public function store(Request $request, string $business)
  15. {
  16. permit('businessProjects');
  17. Project::create($request->merge(['business_id' => $business])->all());
  18. return Business::info($request->route('business'), true);
  19. }
  20. public function update(Request $request,string $project)
  21. {
  22. permit('projectEdit', ['project_id' => $project]);
  23. $project = Project::findOrFail($project);
  24. $project->update($request->except('business_id'));
  25. return Business::info($request->route('business'), true);
  26. }
  27. public function delete(Request $request, string $project)
  28. {
  29. permit('businessProjects');
  30. $project = Project::findOrFail($project);
  31. $project->delete();
  32. return Business::info($request->route('business'));
  33. }
  34. public function restore(Request $request, string $project)
  35. {
  36. $project = Project::onlyTrashed()->findOrFail($project);
  37. $project->restore();
  38. return response(['message' => 'project successfully restored.']);
  39. }
  40. public function storeOrUpdateUser($business, $project, Request $request)
  41. {
  42. permit('projectUsers', ['project_id' => $project]);
  43. $validatedData = $this->validate($request, [
  44. 'level' => 'required|numeric|between:1,3',
  45. 'user_id' => 'required|numeric|not_in:'.auth()->id(),
  46. ]);
  47. $this->checkAddUserPolicy($request->user_id, $request->level);
  48. $projectModel = Project::findOrFail($project);
  49. DB::transaction(function () use ($business, $validatedData, $request, $projectModel) {
  50. $projectModel->members()->sync([$request->user_id => $validatedData], false);
  51. if (!can('businessAccess', ['user_id' => $request->user_id])) {
  52. // Register user to business with zero level
  53. //User not exist in the business before
  54. $this->addUserWithZeroLevel($request->user_id, $business);
  55. }
  56. }, 3);
  57. return Business::info($projectModel->business_id, true);
  58. }
  59. public function checkAddUserPolicy($user, $level)
  60. {
  61. if (can('businessAccess', ['user_id' => $user])
  62. && $level < request('_business_info')['info']['users'][$user]['level']) {// before in business
  63. abort(405);
  64. }
  65. }
  66. public function addUserWithZeroLevel($user_id, $business)
  67. {
  68. $businessModel = Business::findOrFail($business);
  69. return $businessModel->users()->sync([$user_id => [
  70. 'level' => 0,
  71. 'user_id' => $user_id
  72. ]], false);
  73. }
  74. public function deleteUser($business, $project, $user)
  75. {
  76. permit('projectAccess', ['project_id' => $project]);
  77. $this->checkDeleteUserPolicy($user, $project);
  78. $projectModel = Project::findOrFail($project);
  79. DB::transaction(function () use ($project, $business, $user, $projectModel) {
  80. $this->detachMember($projectModel, $user);
  81. if (!can('isActiveUser', ['user_id' => $user]) && !$this->haveOneProject($user, $project)) {
  82. // User level in business is zero
  83. // And haven't another project then remove it form business
  84. $businessModel = Business::findOrFail($business);
  85. $this->detachUser($businessModel, $user);
  86. }
  87. }, 3);
  88. return Business::info($projectModel->business_id, true);
  89. }
  90. public function detachMember($project, $user)
  91. {
  92. return $project->members()->detach($user) ? true : abort(404);
  93. }
  94. public function detachUser($business, $user)
  95. {
  96. return $business->users()->detach($user) ? true : abort(404);
  97. }
  98. public function haveOneProject($user, $project)
  99. {
  100. foreach (request('_business_info')['info']['projects'] as $id => $item) {
  101. if ($item['members'][$user]['level'] > enum('levels.inactive.id') && $id != $project) {
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. public function checkDeleteUserPolicy($user, $project)
  108. {
  109. if (!can('isProjectOwner', ['project_id' => $project]) && (auth()->id() != $user) ) {
  110. abort(405);
  111. }
  112. }
  113. public function setAvatar(Request $request, string $project)
  114. {
  115. $project = Project::findOrFail($project);
  116. if ($request->hasFile('avatar')) {
  117. $project->saveAsAvatar($request->file('avatar'));
  118. }
  119. return $project;
  120. }
  121. public function unSetAvatar(Request $request, string $project)
  122. {
  123. $project = Project::findOrFail($project);
  124. $project->deleteAvatar();
  125. return $project;
  126. }
  127. }