Пример #1
0
 private static function canViewPrivateBin(Bin $bin, $request)
 {
     // Check if user is admin
     if (auth()->check() && auth()->user()->admin()) {
         return true;
     }
     // Check if user is bin owner
     if (auth()->check() && auth()->user()->getAuthIdentifier() == $bin->user_id) {
         return true;
     }
     if ($bin->isShared()) {
         if (session()->get('private-' . $bin->getRouteKey()) == $bin->private_hash) {
             return true;
         }
         // Check if user has a hash key for bin
         $hash = $request->route('hash');
         if ($hash) {
             if ($bin->private_hash == $hash) {
                 if (!session()->has('private-' . $bin->getRouteKey()) || session()->get('private-' . $bin->getRouteKey()) !== $bin->private_hash) {
                     session(['private-' . $bin->getRouteKey() => $bin->private_hash]);
                 }
                 return true;
             }
         }
     }
     return false;
 }
Пример #2
0
 public function deletePost(Bin $bin, Comment $comment)
 {
     $comment->delete();
     session()->flash('success', 'Comment deleted successfully!');
     return redirect()->route('bin.code', $bin->getRouteKey());
 }
Пример #3
0
 public function editPost(Bin $bin, Requests\Bins\CreateBin $request)
 {
     $description = $request->has('description') && trim($request->input('description')) != '' ? $request->input('description') : null;
     // Update the bin itself
     $bin->title = $request->input('title');
     $bin->description = $description;
     $bin->visibility = $request->input('visibility');
     $bin->save();
     $bin->versions()->sync($request->input('versions'));
     // Files that currently exist with the bin object
     $existingFiles = [];
     foreach ($bin->snippets as $file) {
         $existingFiles[] = $file->getRouteKey();
     }
     /**
      * Now we need to check that the existing files are still
      * present in the post, if they are then they are updated
      * if they are not, then they are deleted.
      */
     $updatedExisting = [];
     $files = [];
     if ($request->has('file')) {
         foreach ($request->input('file') as $key => $value) {
             $files[$key]['file'] = $value;
             $updatedExisting[] = $value;
         }
     }
     foreach ($request->input('name') as $key => $value) {
         $files[$key]['name'] = $value;
     }
     foreach ($request->input('language') as $key => $value) {
         $files[$key]['language'] = $value;
     }
     foreach ($request->input('code') as $key => $value) {
         $files[$key]['code'] = $value;
     }
     $filesToDelete = [];
     foreach ($existingFiles as $key => $value) {
         if (!in_array($value, $updatedExisting)) {
             /**
              * This previously existing file was not
              * found in the update. User wants to delete it.
              */
             $filesToDelete[] = hashid()->decode($value)[0];
         }
     }
     Snippet::where('bin_id', $bin->id)->whereIn('id', $filesToDelete)->delete();
     foreach ($files as $item) {
         $type = Type::where('css_class', $item['language'])->first();
         if (array_key_exists('file', $item)) {
             // update existing snippet
             $snippet = Snippet::where(['bin_id' => $bin->id, 'id' => hashid()->decode($item['file'])])->first();
             $snippet->type_id = $type->id;
             $snippet->name = $item['name'];
             $snippet->code = $item['code'];
             $snippet->save();
         } else {
             // create new snippet
             $bin->snippets()->create(['type_id' => $type->id, 'name' => $item['name'], 'code' => $item['code']]);
         }
     }
     session()->flash('success', 'Bin updated successfully!');
     return redirect()->route('bin.code', $bin->getRouteKey());
 }