faithgen /
testimonies
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Faithgen\Testimonies\Http\Controllers; |
||
| 4 | |||
| 5 | use FaithGen\SDK\Helpers\CommentHelper; |
||
| 6 | use FaithGen\SDK\Http\Requests\CommentRequest; |
||
| 7 | use FaithGen\SDK\Models\Image; |
||
| 8 | use FaithGen\SDK\Models\User; |
||
| 9 | use Faithgen\Testimonies\Http\Requests\AddImagesRequest; |
||
| 10 | use Faithgen\Testimonies\Http\Requests\CreateRequest; |
||
| 11 | use Faithgen\Testimonies\Http\Requests\DeleteImageRequest; |
||
| 12 | use Faithgen\Testimonies\Http\Requests\ToggleApprovalRequest; |
||
| 13 | use Faithgen\Testimonies\Http\Requests\UpdateRequest; |
||
| 14 | use Faithgen\Testimonies\Http\Resources\Testimony as TestimonyResource; |
||
| 15 | use Faithgen\Testimonies\Http\Resources\TestimonyDetails; |
||
| 16 | use Faithgen\Testimonies\Jobs\ProcessImages; |
||
| 17 | use Faithgen\Testimonies\Jobs\S3Upload; |
||
| 18 | use Faithgen\Testimonies\Jobs\UploadImages; |
||
| 19 | use Faithgen\Testimonies\Models\Testimony; |
||
| 20 | use Faithgen\Testimonies\Services\TestimoniesService; |
||
| 21 | use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
||
| 22 | use Illuminate\Foundation\Bus\DispatchesJobs; |
||
| 23 | use Illuminate\Foundation\Validation\ValidatesRequests; |
||
| 24 | use Illuminate\Http\Request; |
||
| 25 | use Illuminate\Routing\Controller; |
||
| 26 | use InnoFlash\LaraStart\Helper; |
||
| 27 | use InnoFlash\LaraStart\Http\Requests\IndexRequest; |
||
| 28 | use InnoFlash\LaraStart\Traits\APIResponses; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Controlls \testimonies. |
||
| 32 | */ |
||
| 33 | final class TestimonyController extends Controller |
||
| 34 | { |
||
| 35 | use ValidatesRequests, AuthorizesRequests, DispatchesJobs, APIResponses; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Injects the testimonies service class. |
||
| 39 | * |
||
| 40 | * @var TestimoniesService |
||
| 41 | */ |
||
| 42 | private TestimoniesService $testimoniesService; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 43 | |||
| 44 | /** |
||
| 45 | * Injects the service class. |
||
| 46 | * |
||
| 47 | * @param TestimoniesService $testimoniesService |
||
| 48 | */ |
||
| 49 | public function __construct(TestimoniesService $testimoniesService) |
||
| 50 | { |
||
| 51 | $this->testimoniesService = $testimoniesService; |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Creates a testimony for the given user. |
||
| 56 | * |
||
| 57 | * @param CreateRequest $request |
||
| 58 | * |
||
| 59 | * @return void |
||
| 60 | */ |
||
| 61 | public function create(CreateRequest $request) |
||
| 62 | { |
||
| 63 | $testifier = auth('web')->user(); |
||
| 64 | $params = array_merge($request->validated(), [ |
||
| 65 | 'ministry_id' => auth()->user()->id, |
||
| 66 | ]); |
||
| 67 | unset($params['images']); |
||
| 68 | try { |
||
| 69 | $testifier->testimonies()->create($params); |
||
| 70 | |||
| 71 | return $this->successResponse('Testimony was posted successfully, waiting for admin to approve!'); |
||
| 72 | } catch (\Exception $exc) { |
||
| 73 | return abort(500, $exc->getMessage()); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Fetches the testimonies. |
||
| 79 | * |
||
| 80 | * @param IndexRequest $request |
||
| 81 | * |
||
| 82 | * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection |
||
| 83 | */ |
||
| 84 | public function index(IndexRequest $request) |
||
| 85 | { |
||
| 86 | $testimonies = auth() |
||
| 87 | ->user() |
||
| 88 | ->testimonies() |
||
| 89 | ->latest() |
||
| 90 | ->where(function ($testimony) use ($request) { |
||
| 91 | return $testimony->search(['title', 'created_at'], $request->filter_text) |
||
| 92 | ->orWhereHas('user', fn($user) => $user->search('name', $request->filter_text)); |
||
| 93 | }) |
||
| 94 | ->with(['user.image', 'images']) |
||
| 95 | ->exclude(['testimony', 'resource']) |
||
| 96 | ->approved() |
||
| 97 | ->paginate(Helper::getLimit($request)); |
||
| 98 | |||
| 99 | TestimonyResource::wrap('testimonies'); |
||
| 100 | |||
| 101 | return TestimonyResource::collection($testimonies); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Retrieves the testimony details. |
||
| 106 | * |
||
| 107 | * Shows only to the owner ministry |
||
| 108 | * |
||
| 109 | * @param Testimony $testimony |
||
| 110 | * |
||
| 111 | * @return TestimonyDetails |
||
| 112 | * @throws \Illuminate\Auth\Access\AuthorizationException |
||
| 113 | */ |
||
| 114 | public function show(Testimony $testimony) |
||
| 115 | { |
||
| 116 | $this->authorize('view', $testimony); |
||
| 117 | TestimonyDetails::withoutWrapping(); |
||
| 118 | |||
| 119 | return new TestimonyDetails($testimony); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Deletes the testimony. |
||
| 124 | * |
||
| 125 | * @param Testimony $testimony |
||
| 126 | * |
||
| 127 | * @return void |
||
| 128 | * @throws \Illuminate\Auth\Access\AuthorizationException |
||
| 129 | */ |
||
| 130 | public function destroy(Testimony $testimony) |
||
| 131 | { |
||
| 132 | $this->authorize('delete', $testimony); |
||
| 133 | try { |
||
| 134 | $testimony->delete(); |
||
| 135 | |||
| 136 | return $this->successResponse('Testimony deleted!'); |
||
| 137 | } catch (\Exception $e) { |
||
| 138 | return abort(500, $e->getMessage()); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Approves and disapprove a testimony. |
||
| 144 | * |
||
| 145 | * @param ToggleApprovalRequest $request |
||
| 146 | * |
||
| 147 | * @return void |
||
| 148 | */ |
||
| 149 | public function toggleApproval(ToggleApprovalRequest $request) |
||
| 150 | { |
||
| 151 | return $this->testimoniesService->update($request->validated(), 'Testimony approval status updated'); |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Fetches testimonies for a given user id who belongs to the authenticated ministry. |
||
| 156 | * |
||
| 157 | * @param Request $request You may include a limit in the request |
||
| 158 | * @param User $user |
||
| 159 | * |
||
| 160 | * @return void |
||
| 161 | */ |
||
| 162 | public function userTestimonies(Request $request, User $user) |
||
| 163 | { |
||
| 164 | if (auth()->user()->ministryUsers()->where('user_id', $user->id)->first()) { |
||
| 165 | $testimonies = auth() |
||
| 166 | ->user() |
||
| 167 | ->testimonies() |
||
| 168 | ->where(function ($testimony) use ($request, $user) { |
||
| 169 | return $testimony->where('user_id', $user->id); |
||
| 170 | }) |
||
| 171 | ->with(['user', 'images']) |
||
| 172 | ->approved($user) |
||
| 173 | ->latest() |
||
| 174 | ->paginate(Helper::getLimit($request)); |
||
| 175 | |||
| 176 | TestimonyResource::wrap('testimonies'); |
||
| 177 | |||
| 178 | return TestimonyResource::collection($testimonies); |
||
| 179 | } |
||
| 180 | |||
| 181 | return abort(403, 'You are not allowed to view testimonies from this user'); |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Updates the user,s testimony here. |
||
| 186 | * |
||
| 187 | * @param UpdateRequest $request |
||
| 188 | * |
||
| 189 | * @return void |
||
| 190 | */ |
||
| 191 | public function update(UpdateRequest $request) |
||
| 192 | { |
||
| 193 | return $this->testimoniesService->update($request->validated(), 'Testimony updated successfully!'); |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Deletes an image from a testimony. |
||
| 198 | * |
||
| 199 | * @param DeleteImageRequest $request |
||
| 200 | * |
||
| 201 | * @param \Faithgen\Testimonies\Http\Controllers\Image $image |
||
| 202 | * |
||
| 203 | * @return void |
||
| 204 | */ |
||
| 205 | public function destroyImage(DeleteImageRequest $request, Image $image) |
||
| 206 | { |
||
| 207 | $image = $this->testimoniesService->getTestimony()->images()->findOrFail($image->id); |
||
| 208 | try { |
||
| 209 | unlink(storage_path('app/public/testimonies/100-100/'.$image->name)); |
||
| 210 | unlink(storage_path('app/public/testimonies/50-50/'.$image->name)); |
||
| 211 | unlink(storage_path('app/public/testimonies/original/'.$image->name)); |
||
| 212 | |||
| 213 | return $this->successResponse('Image deleted!'); |
||
| 214 | } catch (\Exception $e) { |
||
| 215 | abort(500, $e->getMessage()); |
||
| 216 | } finally { |
||
| 217 | $image->delete(); |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Uploads images attaching them to a given testimony. |
||
| 223 | * |
||
| 224 | * @param AddImagesRequest $request |
||
| 225 | * |
||
| 226 | * @return void |
||
| 227 | */ |
||
| 228 | public function addImages(AddImagesRequest $request) |
||
| 229 | { |
||
| 230 | UploadImages::withChain([ |
||
| 231 | new ProcessImages($this->testimoniesService->getTestimony()), |
||
| 232 | new S3Upload($this->testimoniesService->getTestimony()), |
||
| 233 | ])->dispatch( |
||
| 234 | $this->testimoniesService->getTestimony(), |
||
| 235 | $request->images |
||
| 236 | ); |
||
| 237 | |||
| 238 | return $this->successResponse('Images uploaded, processing them now'); |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Fetches comments for the given testimony. |
||
| 243 | * |
||
| 244 | * @param Request $request |
||
| 245 | * @param Testimony $testimony |
||
| 246 | * |
||
| 247 | * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection |
||
| 248 | * @throws \Illuminate\Auth\Access\AuthorizationException |
||
| 249 | */ |
||
| 250 | public function comments(Request $request, Testimony $testimony) |
||
| 251 | { |
||
| 252 | $this->authorize('view', $testimony); |
||
| 253 | |||
| 254 | return CommentHelper::getComments($testimony, $request); |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * This sends a comment to the given testimony. |
||
| 259 | * |
||
| 260 | * @param CommentRequest $request |
||
| 261 | * |
||
| 262 | * @return \Illuminate\Http\JsonResponse |
||
| 263 | */ |
||
| 264 | public function comment(CommentRequest $request) |
||
| 265 | { |
||
| 266 | return CommentHelper::createComment($this->testimoniesService->getTestimony(), $request); |
||
| 267 | } |
||
| 268 | } |
||
| 269 |