Changeset 7304c7f


Ignore:
Timestamp:
09/27/21 23:25:43 (3 years ago)
Author:
beratkjufliju <kufliju@…>
Branches:
develop, master
Children:
582789f
Parents:
2fc88ec
Message:

added user authentication, create & forgot password methods and blades

Files:
793 added
5 deleted
13 edited
3 moved

Legend:

Unmodified
Added
Removed
  • app/Http/Kernel.php

    r2fc88ec r7304c7f  
    2222        \App\Http\Middleware\TrimStrings::class,
    2323        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
     24        \App\Http\Middleware\CheckForMaintenanceMode::class,
    2425    ];
    2526
     
    6465        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    6566        'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
     67        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
     68        'createPassword' => \App\Http\Middleware\CheckCreatePassword::class,
     69        'checkVerifyNewEmail' => \App\Http\Middleware\CheckVerifyNewEmail::class,
     70        'permission' => \App\Http\Middleware\CheckPermission::class,
     71        'checkIsActive' => \App\Http\Middleware\CheckIsActive::class,
    6672    ];
    6773}
  • app/Http/Middleware/Authenticate.php

    r2fc88ec r7304c7f  
    1616    {
    1717        if (! $request->expectsJson()) {
    18             return route('login');
     18            return route('auth.loginShow');
    1919        }
    2020    }
  • app/Http/Middleware/TrimStrings.php

    r2fc88ec r7304c7f  
    1313     */
    1414    protected $except = [
    15         'current_password',
    1615        'password',
    1716        'password_confirmation',
  • app/Http/Middleware/TrustHosts.php

    r2fc88ec r7304c7f  
    44
    55use Illuminate\Http\Middleware\TrustHosts as Middleware;
     6use Illuminate\Http\Request;
    67
    78class TrustHosts extends Middleware
  • app/Http/Middleware/VerifyCsrfToken.php

    r2fc88ec r7304c7f  
    88{
    99    /**
     10     * Indicates whether the XSRF-TOKEN cookie should be set on the response.
     11     *
     12     * @var bool
     13     */
     14    protected $addHttpCookie = true;
     15
     16    /**
    1017     * The URIs that should be excluded from CSRF verification.
    1118     *
  • app/Models/User.php

    r2fc88ec r7304c7f  
    33namespace App\Models;
    44
    5 use Illuminate\Contracts\Auth\MustVerifyEmail;
    6 use Illuminate\Database\Eloquent\Factories\HasFactory;
     5use Illuminate\Support\Str;
     6use Illuminate\Notifications\Notifiable;
    77use Illuminate\Foundation\Auth\User as Authenticatable;
    8 use Illuminate\Notifications\Notifiable;
    9 use Laravel\Sanctum\HasApiTokens;
    108
    119class User extends Authenticatable
    1210{
    13     use HasApiTokens, HasFactory, Notifiable;
     11    use Notifiable;
     12
     13    protected $table = "users";
    1414
    1515    /**
    1616     * The attributes that are mass assignable.
    1717     *
    18      * @var string[]
     18     * @var array
    1919     */
    2020    protected $fillable = [
    21         'name',
    22         'email',
    23         'password',
     21        "name",
     22        "surname",
     23        "username",
     24        "password",
     25        "email",
     26        "country_code",
     27        "mobile_number",
     28        "role_id"
    2429    ];
    2530
    2631    /**
    27      * The attributes that should be hidden for serialization.
     32     * The attributes that should be hidden for arrays.
    2833     *
    2934     * @var array
    3035     */
    3136    protected $hidden = [
    32         'password',
    33         'remember_token',
     37        "password",
     38        "remember_token",
     39        "is_active"
    3440    ];
    3541
    3642    /**
    37      * The attributes that should be cast.
     43     * The attributes that should be cast to native types.
    3844     *
    3945     * @var array
    4046     */
    41     protected $casts = [
    42         'email_verified_at' => 'datetime',
    43     ];
     47    protected $casts = [];
     48
     49
     50    public function role() {
     51        return $this->belongsTo(Role::class);
     52    }
     53
     54    public function post() {
     55        return $this->hasMany(Post::class);
     56    }
     57
     58    public function comments()
     59    {
     60        return $this->hasManyThrough(Comment::class, Post::class);
     61    }
     62
     63    public function hasPermission($permission, $id = null, $any = false) {
     64
     65        $userPermissions = null;
     66        $flag = null;
     67
     68        if($id != null) $userPermissions = User::find($id)->role->permission->pluck("name");
     69        else $userPermissions = $this->role->permission->pluck("name");
     70
     71        if($any) {
     72            foreach($permission as $p) {
     73                if($this->hasPermission($p)) {
     74                    return true;
     75                }
     76            }
     77            return false;
     78        }
     79
     80        if(is_string($permission)) {
     81            return $userPermissions->contains($permission);
     82        }
     83
     84        if(is_array($permission)) {
     85            foreach($permission as $p) {
     86                if($this->hasPermission($p)) {
     87                    $flag = true;
     88                } else {
     89                    $flag = false; break;
     90                }
     91            }
     92        }
     93
     94        return $flag;
     95    }
     96
     97    public function hasAllPermissions(array $permissions = array(), $id = null, $any = false) {
     98        return $this->hasPermission($permissions, $id, $any);
     99    }
     100
     101    public function hasAnyPermission(array $permissions = array(), $id = null, $any = true) {
     102        return $this->hasPermission($permissions, $id, $any);
     103    }
     104
     105    public function hasRole($role) {
     106        return $role === $this->role->name;
     107    }
     108
     109    public function isAdmin() {
     110        return $this->hasRole("admin");
     111    }
     112
     113    public function isAdminOrEditor() {
     114        return $this->hasRole("admin") || $this->hasRole("editor");
     115    }
     116
     117    public function getFullName($id = null) {
     118
     119        if($id != null) {
     120            $user = User::find($id);
     121            return $user->name . " " . $user->surname;
     122        }
     123
     124        return $this->name . " " . $this->surname;
     125    }
     126
     127    public function getPostsCount($id) {
     128        return Post::where("user_id", $id)->count();
     129    }
     130
     131    public function generateTemporaryPassword($length = 20) {
     132        return bcrypt(Str::random($length));
     133    }
     134
     135    public function generateSecurityCode($min = 10000, $max = 99999) {
     136        return rand($min, $max);
     137    }
     138
     139    public function generateVerifyToken($length = 32) {
     140        return Str::random($length);
     141    }
    44142}
  • app/Providers/AuthServiceProvider.php

    r2fc88ec r7304c7f  
    1414     */
    1515    protected $policies = [
    16         // 'App\Models\Model' => 'App\Policies\ModelPolicy',
     16        'App\Models\Model' => 'App\Policies\ModelPolicy',
    1717    ];
    1818
  • app/Providers/RouteServiceProvider.php

    r2fc88ec r7304c7f  
    2727     * @var string|null
    2828     */
    29     // protected $namespace = 'App\\Http\\Controllers';
     29    protected $namespace = 'App\\Http\\Controllers';
    3030
    3131    /**
  • database/migrations/2021_09_27_171104_create_roles_table.php

    r2fc88ec r7304c7f  
    11<?php
    22
     3use Illuminate\Support\Facades\Schema;
     4use Illuminate\Database\Schema\Blueprint;
    35use Illuminate\Database\Migrations\Migration;
    4 use Illuminate\Database\Schema\Blueprint;
    5 use Illuminate\Support\Facades\Schema;
    66
    7 class CreateUsersTable extends Migration
     7class CreateRolesTable extends Migration
    88{
    99    /**
     
    1414    public function up()
    1515    {
    16         Schema::create('users', function (Blueprint $table) {
    17             $table->id();
     16        Schema::create('roles', function (Blueprint $table) {
     17            $table->increments('id');
    1818            $table->string('name');
    19             $table->string('email')->unique();
    20             $table->timestamp('email_verified_at')->nullable();
    21             $table->string('password');
    22             $table->rememberToken();
    23             $table->timestamps();
    2419        });
    2520    }
     
    3227    public function down()
    3328    {
    34         Schema::dropIfExists('users');
     29        Schema::dropIfExists('roles');
    3530    }
    3631}
  • database/migrations/2021_09_27_171108_create_password_resets_table.php

    r2fc88ec r7304c7f  
    11<?php
    22
     3use Illuminate\Support\Facades\Schema;
     4use Illuminate\Database\Schema\Blueprint;
    35use Illuminate\Database\Migrations\Migration;
    4 use Illuminate\Database\Schema\Blueprint;
    5 use Illuminate\Support\Facades\Schema;
    66
    77class CreatePasswordResetsTable extends Migration
  • database/seeders/DatabaseSeeder.php

    r2fc88ec r7304c7f  
    1414    public function run()
    1515    {
    16         // \App\Models\User::factory(10)->create();
     16        $this->call(RolesTableSeeder::class);
     17        $this->call(PermissionsTableSeeder::class);
     18        $this->call(RolesPermissionsTableSeeder::class);
     19        $this->call(UsersTableSeeder::class);
    1720    }
    1821}
  • package-lock.json

    r2fc88ec r7304c7f  
    15521552            "dev": true
    15531553        },
     1554        "adjust-sourcemap-loader": {
     1555            "version": "4.0.0",
     1556            "resolved": "https://registry.npmjs.org/adjust-sourcemap-loader/-/adjust-sourcemap-loader-4.0.0.tgz",
     1557            "integrity": "sha512-OXwN5b9pCUXNQHJpwwD2qP40byEmSgzj8B4ydSN0uMNYWiFmJ6x6KwUllMmfk8Rwu/HJDFR7U8ubsWBoN0Xp0A==",
     1558            "dev": true,
     1559            "requires": {
     1560                "loader-utils": "^2.0.0",
     1561                "regex-parser": "^2.2.11"
     1562            },
     1563            "dependencies": {
     1564                "loader-utils": {
     1565                    "version": "2.0.0",
     1566                    "resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-2.0.0.tgz",
     1567                    "integrity": "sha512-rP4F0h2RaWSvPEkD7BLDFQnvSf+nK+wr3ESUjNTyAGobqrijmW92zc+SO6d4p4B1wh7+B/Jg1mkQe5NYUEHtHQ==",
     1568                    "dev": true,
     1569                    "requires": {
     1570                        "big.js": "^5.2.2",
     1571                        "emojis-list": "^3.0.0",
     1572                        "json5": "^2.1.2"
     1573                    }
     1574                }
     1575            }
     1576        },
    15541577        "aggregate-error": {
    15551578            "version": "3.1.0",
     
    56325655            }
    56335656        },
     5657        "regex-parser": {
     5658            "version": "2.2.11",
     5659            "resolved": "https://registry.npmjs.org/regex-parser/-/regex-parser-2.2.11.tgz",
     5660            "integrity": "sha512-jbD/FT0+9MBU2XAZluI7w2OBs1RBi6p9M83nkoZayQXXU9e8Robt69FcZc7wU4eJD/YFTjn1JdCk3rbMJajz8Q==",
     5661            "dev": true
     5662        },
    56345663        "regexp.prototype.flags": {
    56355664            "version": "1.3.1",
     
    57285757            "dev": true
    57295758        },
     5759        "resolve-url-loader": {
     5760            "version": "4.0.0",
     5761            "resolved": "https://registry.npmjs.org/resolve-url-loader/-/resolve-url-loader-4.0.0.tgz",
     5762            "integrity": "sha512-05VEMczVREcbtT7Bz+C+96eUO5HDNvdthIiMB34t7FcF8ehcu4wC0sSgPUubs3XW2Q3CNLJk/BJrCU9wVRymiA==",
     5763            "dev": true,
     5764            "requires": {
     5765                "adjust-sourcemap-loader": "^4.0.0",
     5766                "convert-source-map": "^1.7.0",
     5767                "loader-utils": "^2.0.0",
     5768                "postcss": "^7.0.35",
     5769                "source-map": "0.6.1"
     5770            },
     5771            "dependencies": {
     5772                "loader-utils": {
     5773                    "version": "2.0.0",
     5774                    "resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-2.0.0.tgz",
     5775                    "integrity": "sha512-rP4F0h2RaWSvPEkD7BLDFQnvSf+nK+wr3ESUjNTyAGobqrijmW92zc+SO6d4p4B1wh7+B/Jg1mkQe5NYUEHtHQ==",
     5776                    "dev": true,
     5777                    "requires": {
     5778                        "big.js": "^5.2.2",
     5779                        "emojis-list": "^3.0.0",
     5780                        "json5": "^2.1.2"
     5781                    }
     5782                },
     5783                "nanocolors": {
     5784                    "version": "0.2.10",
     5785                    "resolved": "https://registry.npmjs.org/nanocolors/-/nanocolors-0.2.10.tgz",
     5786                    "integrity": "sha512-i+EDWGsJClQwR/bhLIG/CObZZwaYaS5qt+yjxZbfV+77QiNHNzE9nj4d9Ut1TGZ0R0eSwPcQWzReASzXuw/7oA==",
     5787                    "dev": true
     5788                },
     5789                "postcss": {
     5790                    "version": "7.0.38",
     5791                    "resolved": "https://registry.npmjs.org/postcss/-/postcss-7.0.38.tgz",
     5792                    "integrity": "sha512-wNrSHWjHDQJR/IZL5IKGxRtFgrYNaAA/UrkW2WqbtZO6uxSLMxMN+s2iqUMwnAWm3fMROlDYZB41dr0Mt7vBwQ==",
     5793                    "dev": true,
     5794                    "requires": {
     5795                        "nanocolors": "^0.2.2",
     5796                        "source-map": "^0.6.1"
     5797                    }
     5798                },
     5799                "source-map": {
     5800                    "version": "0.6.1",
     5801                    "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
     5802                    "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==",
     5803                    "dev": true
     5804                }
     5805            }
     5806        },
    57305807        "retry": {
    57315808            "version": "0.13.1",
     
    57795856            "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==",
    57805857            "dev": true
     5858        },
     5859        "sass": {
     5860            "version": "1.42.1",
     5861            "resolved": "https://registry.npmjs.org/sass/-/sass-1.42.1.tgz",
     5862            "integrity": "sha512-/zvGoN8B7dspKc5mC6HlaygyCBRvnyzzgD5khiaCfglWztY99cYoiTUksVx11NlnemrcfH5CEaCpsUKoW0cQqg==",
     5863            "dev": true,
     5864            "requires": {
     5865                "chokidar": ">=3.0.0 <4.0.0"
     5866            }
     5867        },
     5868        "sass-loader": {
     5869            "version": "12.1.0",
     5870            "resolved": "https://registry.npmjs.org/sass-loader/-/sass-loader-12.1.0.tgz",
     5871            "integrity": "sha512-FVJZ9kxVRYNZTIe2xhw93n3xJNYZADr+q69/s98l9nTCrWASo+DR2Ot0s5xTKQDDEosUkatsGeHxcH4QBp5bSg==",
     5872            "dev": true,
     5873            "requires": {
     5874                "klona": "^2.0.4",
     5875                "neo-async": "^2.6.2"
     5876            }
    57815877        },
    57825878        "schema-utils": {
  • package.json

    r2fc88ec r7304c7f  
    1414        "laravel-mix": "^6.0.6",
    1515        "lodash": "^4.17.19",
    16         "postcss": "^8.1.14"
     16        "postcss": "^8.1.14",
     17        "resolve-url-loader": "^4.0.0",
     18        "sass": "^1.42.1",
     19        "sass-loader": "^12.1.0"
    1720    }
    1821}
  • routes/web.php

    r2fc88ec r7304c7f  
    1414*/
    1515
    16 Route::get('/', function () {
    17     return view('welcome');
     16////////////////////
     17// Auth Routes
     18////////////////////
     19Route::prefix('auth')->group(function () {
     20
     21    Route::get('/login', "Auth\LoginController@showLogin")->name("auth.loginShow");
     22    Route::post('/login', "Auth\LoginController@login")->name("auth.login");
     23    Route::post('/logout', "Auth\LoginController@logout")->name("auth.logout");
     24
     25    Route::get('/forgot', "Auth\ForgotPasswordController@showForgotPassword")->name("auth.forgotShow");
     26    Route::post('/forgot', "Auth\ForgotPasswordController@forgotPassword")->name("auth.forgot");
     27
     28    Route::group(['middleware' => "createPassword"], function () {
     29        Route::get('/create-password/{id}/{token}', "Auth\CreatePasswordController@showCreatePassword")->name("auth.create-password-show");
     30        Route::post('/create-password/{id}/{token}', "Auth\CreatePasswordController@createPassword")->name("auth.create-password");
     31    });
     32
     33    Route::group(['middleware' => "checkVerifyNewEmail"], function () {
     34        Route::get('/verify/{id}/{token}', "Auth\VerifyNewEmailController@create")->name("auth.verifyShow");
     35        Route::post('/verify/{id}/{token}', "Auth\VerifyNewEmailController@verify")->name("auth.verify");
     36    });
     37
    1838});
  • webpack.mix.js

    r2fc88ec r7304c7f  
    1212 */
    1313
    14 mix.js('resources/js/app.js', 'public/js')
    15     .postCss('resources/css/app.css', 'public/css', [
    16         //
    17     ]);
     14mix.js('resources/assets/js/app.js', 'public/assets/js')
     15    .sass('resources/assets/sass/app.scss', 'public/assets/css');
     16
     17mix.styles([
     18    'resources/assets/css/app.css'
     19],  'public/assets/css/app.min.css');
     20
     21mix.copy(['resources/assets/media/images'
     22],  'public/assets/media/images');
Note: See TracChangeset for help on using the changeset viewer.