Skip to content

Commit 92d7680

Browse files
committed
Edits the role_user pivot table
1 parent 1f3a444 commit 92d7680

File tree

5 files changed

+43
-25
lines changed

5 files changed

+43
-25
lines changed

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ To check for a role:
5555
```php
5656
$user->hasRole('admin');
5757
```
58-
or
59-
```php
60-
$user->is('admin');
61-
```
6258

6359
To check for permissions:
6460
```php

migrations/2017_02_05_092833_create_roles_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function up()
1515
{
1616
Schema::create('roles', function (Blueprint $table) {
1717
$table->increments('id');
18-
$table->string('name');
18+
$table->string('name')->unique();
1919
$table->timestamps();
2020
});
2121
}

migrations/2017_02_05_093559_create_users_roles_table.php renamed to migrations/2017_02_05_093559_create_role_user_table.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use Illuminate\Database\Schema\Blueprint;
55
use Illuminate\Database\Migrations\Migration;
66

7-
class CreateUsersRolesTable extends Migration
7+
class CreateRoleUserTable extends Migration
88
{
99
/**
1010
* Run the migrations.
@@ -13,14 +13,16 @@ class CreateUsersRolesTable extends Migration
1313
*/
1414
public function up()
1515
{
16-
Schema::create('users_roles', function (Blueprint $table) {
17-
$table->integer('user_id')->unsigned();
18-
$table->integer('role_id')->unsigned();
16+
Schema::create('role_user', function (Blueprint $table) {
17+
$table->increments('id');
18+
$table->integer('user_id')->unsigned()->index();
19+
$table->integer('role_id')->unsigned()->index();
20+
$table->timestamps();
1921

2022
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
2123
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
2224

23-
$table->primary(['user_id', 'role_id']);
25+
$table->unique(['user_id', 'role_id']);
2426
});
2527
}
2628

src/HasRolesAndPermissions.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Denismitr\Permissions;
44

5+
use App\User;
56
use Denismitr\Permissions\Models\Permission;
67
use Denismitr\Permissions\Models\Role;
78

@@ -90,26 +91,17 @@ public function assignRole(...$roles)
9091
{
9192
foreach ($roles as $role) {
9293
if ( ! $this->hasRole($role) ) {
93-
$this->roles()->create([
94-
'name' => $role
95-
]);
94+
$role = Role::fromName($role);
95+
96+
$this->roles()->attach($role);
97+
98+
$this->load('roles');
9699
}
97100
}
98101

99102
return $this;
100103
}
101104

102-
/**
103-
* Alias for hasRole
104-
*
105-
* @param string $roles
106-
* @return bool
107-
*/
108-
public function is(...$roles)
109-
{
110-
return $this->hasRole(...$roles);
111-
}
112-
113105
/**
114106
* Check if user has role
115107
*
@@ -127,11 +119,13 @@ public function hasRole(...$roles)
127119
return false;
128120
}
129121

122+
130123
public function roles()
131124
{
132-
return $this->belongsToMany(Role::class, 'users_roles');
125+
return $this->belongsToMany(Role::class);
133126
}
134127

128+
135129
public function permissions()
136130
{
137131
return $this->belongsToMany(Permission::class, 'users_permissions');

src/Models/Role.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,40 @@
22

33
namespace Denismitr\Permissions\Models;
44

5+
use App\User;
56
use Illuminate\Database\Eloquent\Model;
67

78
class Role extends Model
89
{
910
protected $guarded = [];
1011

12+
/**
13+
* Create new instance of Role with new name or old
14+
* one if not unique
15+
*
16+
* @param string $name
17+
* @return Illuminate\Database\Eloquent\Model
18+
*/
19+
public static function fromName($name)
20+
{
21+
return self::updateOrCreate([
22+
'name' => $name
23+
]);
24+
}
25+
26+
/**
27+
* Belongs to many permissions
28+
*
29+
* @return Illuminate\Database\Eloquent\Relations\BelongsToMany
30+
*/
1131
public function permissions()
1232
{
1333
return $this->belongsToMany(Permission::class, 'roles_permissions');
1434
}
35+
36+
37+
public function users()
38+
{
39+
return $this->belongsToMany(User::class);
40+
}
1541
}

0 commit comments

Comments
 (0)