]> BookStack Code Mirror - bookstack/commitdiff
Fixed password re-writing and improved book slug creation
authorDan Brown <redacted>
Sun, 9 Aug 2015 09:26:54 +0000 (10:26 +0100)
committerDan Brown <redacted>
Sun, 9 Aug 2015 09:26:54 +0000 (10:26 +0100)
app/Http/Controllers/BookController.php
app/Repos/BookRepo.php
app/User.php

index 7aed33d194d7df6c580e08da9ad59b18588b1f11..6791c0f2a8f611ebeb136c3c485ff46dfe385f63 100644 (file)
@@ -61,11 +61,7 @@ class BookController extends Controller
             'description' => 'string|max:1000'
         ]);
         $book = $this->bookRepo->newFromInput($request->all());
-        $slug = Str::slug($book->name);
-        while($this->bookRepo->countBySlug($slug) > 0) {
-            $slug .= '1';
-        }
-        $book->slug = $slug;
+        $book->slug = $this->bookRepo->findSuitableSlug($book->name);
         $book->created_by = Auth::user()->id;
         $book->updated_by = Auth::user()->id;
         $book->save();
@@ -111,11 +107,7 @@ class BookController extends Controller
             'description' => 'string|max:1000'
         ]);
         $book->fill($request->all());
-        $slug = Str::slug($book->name);
-        while($this->bookRepo->countBySlug($slug) > 0 && $book->slug != $slug) {
-            $slug += '1';
-        }
-        $book->slug = $slug;
+        $book->slug = $this->bookRepo->findSuitableSlug($book->name, $book->id);
         $book->updated_by = Auth::user()->id;
         $book->save();
         return redirect($book->getUrl());
index 5a772539f113bf11dcfe774e307448cb82de2949..488478aac5daf58dbae57df69a22dda29b1033c0 100644 (file)
@@ -1,5 +1,6 @@
 <?php namespace Oxbow\Repos;
 
+use Illuminate\Support\Str;
 use Oxbow\Book;
 
 class BookRepo
@@ -62,4 +63,22 @@ class BookRepo
         return $lastElem ? $lastElem->priority + 1 : 0;
     }
 
+    public function doesSlugExist($slug, $currentId = false)
+    {
+        $query = $this->book->where('slug', '=', $slug);
+        if($currentId) {
+            $query = $query->where('id', '!=', $currentId);
+        }
+        return $query->count() > 0;
+    }
+
+    public function findSuitableSlug($name, $currentId = false)
+    {
+        $slug = Str::slug($name);
+        while($this->doesSlugExist($slug, $currentId)) {
+            $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
+        }
+        return $slug;
+    }
+
 }
\ No newline at end of file
index 1a4e2081064abfd6ded3304b9215d212e687382d..2e37fe484d439f204f166caf2f4c884638bdec07 100644 (file)
@@ -24,7 +24,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
      *
      * @var array
      */
-    protected $fillable = ['name', 'email', 'password'];
+    protected $fillable = ['name', 'email'];
 
     /**
      * The attributes excluded from the model's JSON form.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.