Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP]Dostępy do podstron w Laravel 5.8 po zalogowaniu
northwest
post 17.05.2019, 12:39:22
Post #1





Grupa: Zarejestrowani
Postów: 788
Pomógł: 1
Dołączył: 17.09.2004

Ostrzeżenie: (10%)
X----


Witam serdecznie.
Od paru dni uczę się Laravel i natrafiłem na następujący problem.
W swojej aplikacji chciałbym mieć 3 poziomy dostępu:
1. użytkownik niezalogowany
2. użytkownik zalogowany (rola: user i userPremium)
3. użytkownik administrator (rola: admin)

W swoim projekcie wykorzystuję wbudowane w Laravel funkcje rejestracji i logowania.


Mam następujące migracje:

  1. Schema::create('users', function (Blueprint $table) {
  2. $table->bigIncrements('id');
  3. $table->bigInteger('company_id')->unsigned();
  4. $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
  5. $table->boolean('enable')->default(0);
  6. $table->string('name', 120)->nullable();
  7. $table->string('surname', 120)->nullable();
  8. $table->string('email', 120)->unique();
  9. $table->timestamp('email_verified_at')->nullable();
  10. $table->string('password');
  11. $table->bigInteger('counter')->default(0);
  12. $table->string('url_address', 160);
  13. $table->boolean('isCompany')->default(0);
  14. $table->boolean('isMailing')->default(0);
  15. $table->text('content')->nullable();
  16. $table->string('nip1', 12)->nullable();
  17. $table->string('business1', 120)->nullable();
  18. $table->string('phone1', 60)->nullable();
  19. $table->string('street1', 150)->nullable();
  20. $table->string('number1', 8)->nullable();
  21. $table->string('postal_code1', 12)->nullable();
  22. $table->string('city1', 100)->nullable();
  23. $table->bigInteger('country_id1')->default(0);
  24. $table->bigInteger('provincial_id1')->default(0);
  25. $table->string('nip2', 12)->nullable();
  26. $table->string('business2', 120)->nullable();
  27. $table->string('phone2', 60)->nullable();
  28. $table->string('street2', 150)->nullable();
  29. $table->string('number2', 8)->nullable();
  30. $table->string('postal_code2', 12)->nullable();
  31. $table->string('city2', 100)->nullable();
  32. $table->bigInteger('country_id2')->default(0);
  33. $table->bigInteger('provincial_id2')->default(0);
  34. $table->string('nip3', 12)->nullable();
  35. $table->string('business3', 120)->nullable();
  36. $table->string('phone3', 60)->nullable();
  37. $table->string('street3', 150)->nullable();
  38. $table->string('number3', 8)->nullable();
  39. $table->string('postal_code3', 12)->nullable();
  40. $table->string('city3', 100)->nullable();
  41. $table->bigInteger('country_id3')->default(0);
  42. $table->bigInteger('provincial_id3')->default(0);
  43. $table->decimal('cash', 9, 2)->default(0);
  44. $table->decimal('lng', 10, 8)->default(0);
  45. $table->decimal('lat', 10, 8)->default(0);
  46. $table->boolean('enable_map')->default(0);
  47. $table->rememberToken();
  48. $table->timestamps();
  49. $table->engine = "InnoDB";
  50. });
  51.  
  52.  
  53.  
  54. Schema::create('roles', function (Blueprint $table) {
  55. $table->bigIncrements('id');
  56. $table->string('name');
  57. $table->engine = "InnoDB";
  58. });
  59.  
  60. Schema::create('role_user', function (Blueprint $table) {
  61. $table->bigInteger('user_id')->unsigned();
  62. $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
  63. $table->bigInteger('role_id')->unsigned();
  64. $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
  65. $table->engine = "InnoDB";
  66. });
  67.  
  68.  
  69. DB::table('roles')->insert([
  70. 'name' => $faker->unique()->randomElement(['admin', 'user', 'userPremium']),
  71. ]);
  72.  
  73.  


Mój router wygląda następująco (web.php):
  1. // użytkownicy niezalogowani
  2. Route::get('/', 'FrontendController@index')->name('home');
  3.  
  4. // użytkownicy zalogowani posiadający rolę: user i userPremium
  5. Route::group(['prefix' => 'panel', 'middleware' => 'auth'], function () {
  6. Route::get('/', 'BackendController@index')->name('adminHome')->middleware('verified');
  7. });
  8.  
  9. // użytkownicy zalogowani posiadający rolę: admin
  10. Route::group(['prefix' => 'cms', 'middleware' => 'auth'], function () {
  11. Route::get('/', 'CMSController@cms')->name('index')->middleware('verified');
  12. });
  13.  




Model User.php wygląda następująco:
  1. class User extends Authenticatable implements MustVerifyEmail
  2. {
  3. use Notifiable;
  4. use psCMS\Presenters\UserPresenter;
  5.  
  6. public static $roles = [];
  7.  
  8. /**
  9.   * The attributes that are mass assignable.
  10.   *
  11.   * @var array
  12.   */
  13.  
  14. protected $fillable = ['company_id', 'enable', 'name', 'surname', 'email', 'email_verified_at', 'password', 'counter', 'url_address', 'isCompany', 'isMailing', 'content', 'nip1', 'business1', 'phone1', 'street1', 'number1', 'postal_code1', 'city1', 'country_id1', 'provincial_id1', 'nip2', 'business2', 'phone2', 'street2', 'number2', 'postal_code2', 'city2', 'country_id2', 'provincial_id2', 'nip3', 'business3', 'phone3', 'street3', 'number3', 'postal_code3', 'city3', 'country_id3', 'provincial_id3', 'cash', 'lng', 'lat', 'enable_map', 'remember_token', 'created_at', 'updated_at', 'last_login_at', 'last_login_ip' ];
  15.  
  16.  
  17. /**
  18.   * The attributes that should be hidden for arrays.
  19.   *
  20.   * @var array
  21.   */
  22. protected $hidden = [
  23. 'password', 'remember_token',
  24. ];
  25.  
  26.  
  27. public function photos()
  28. {
  29. return $this->morphMany('App\Photo', 'photoable');
  30. }
  31.  
  32.  
  33. public function roles()
  34. {
  35. return $this->belongsToMany('App\Role');
  36. }
  37.  
  38.  
  39. public function hasRole(array $roles)
  40. {
  41.  
  42. foreach($roles as $role)
  43. {
  44.  
  45. if(isset(self::$roles[$role]))
  46. {
  47. if(self::$roles[$role]) return true;
  48.  
  49. }
  50. else
  51. {
  52. self::$roles[$role] = $this->roles()->where('name', $role)->exists();
  53. if(self::$roles[$role]) return true;
  54. }
  55.  
  56. }
  57.  
  58.  
  59. return false;
  60.  
  61. }
  62.  
  63. }
  64.  


W jaki sposób mogę to zrobić?
1. Użytkownik niezalogowany nie może wejść zarówno do route panel oraz admin
2. Użytkownik zalogowany jako user lub userPremium może wejść tylko do route panel - nie może wejść do route admin
3. Użytkownik zalogowany jako user admin może wejść tylko do route admin - nie może wejść do route panel

W jaki sposób to zrobić?







Go to the top of the page
+Quote Post
Pyton_000
post 17.05.2019, 13:02:23
Post #2





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


Pomijając fakt że masz kupę zbędnych kolumn w bazie....
https://laravel.com/docs/5.8/middleware#mid...ware-parameters
Go to the top of the page
+Quote Post
northwest
post 17.05.2019, 13:31:23
Post #3





Grupa: Zarejestrowani
Postów: 788
Pomógł: 1
Dołączył: 17.09.2004

Ostrzeżenie: (10%)
X----


Chyba coś nie do końca mi działa sad.gif

Dodałem nową klasę:
CheckRole

  1. use App\Role;
  2.  
  3. use Closure;
  4.  
  5. class CheckRole
  6. {
  7. /**
  8.   * Handle an incoming request.
  9.   *
  10.   * @param \Illuminate\Http\Request $request
  11.   * @param \Closure $next
  12.   * @return mixed
  13.   */
  14. public function handle($request, Closure $next, $role)
  15. {
  16. if (! $request->user()->hasRole($role)) {
  17. return redirect()->route('home');
  18. }
  19.  
  20. return $next($request);
  21. }
  22. }



I teraz do routera zrobiłem coś takiego:
  1. // użytkownicy niezalogowani
  2. Route::get('/', 'FrontendController@index')->name('home');
  3.  
  4. // użytkownicy zalogowani posiadający rolę: user i userPremium
  5. Route::group(['prefix' => 'panel', 'middleware' => 'auth'], function () {
  6. Route::get('/', 'BackendController@index')->name('adminHome')->middleware('role:user,role:userPremium');
  7. });
  8.  
  9. // użytkownicy zalogowani posiadający rolę: admin
  10. Route::group(['prefix' => 'cms', 'middleware' => 'auth'], function () {
  11. Route::get('/', 'CMSController@cms')->name('index')->middleware('role:admin');
  12. });



Po uruchomieniu tego kodu otrzymuję:
ReflectionException (-1)
Class role does not exist

w $reflector = new ReflectionClass($concrete);



Screen: https://ibb.co/ZxgDZRj



Co jeszcze muszę dorobić?
Go to the top of the page
+Quote Post
Pyton_000
post 17.05.2019, 13:56:58
Post #4





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


https://laravel.com/docs/5.8/middleware#reg...ring-middleware

Czytaj dokumentację.

Co do tabeli to masz 3 zestawy tych samych danych więc powinna to być oddzielna tabela i złączona relacją 1:n
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 28.03.2024 - 23:22