-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAssignRoleToUser.php
105 lines (94 loc) · 2.48 KB
/
AssignRoleToUser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace App\Livewire\Crew;
use App\Models\User;
use Illuminate\Support\Facades\Gate;
use Illuminate\View\View;
use Livewire\Component;
use LivewireUI\Modal\ModalComponent;
use Spatie\Permission\Models\Role;
class AssignRoleToUser extends ModalComponent
{
public $role;
public $searchValue;
public $isDropdownVisible;
public $users;
public $defaultUsers;
public $selectedUser;
/**
* Initializes component
*
* @param $role
* @return void
*/
public function mount($role) : void
{
if (!Gate::authorize('invite-crew-member')) {
abort(403);
}
$this->role = Role::find($role);
$this->isDropdownVisible = false;
$this->defaultUsers = User::withoutRole($role)
->doesntHave('company')
->orderBy('name')
->get();
$this->users = $this->defaultUsers;
$this->searchValue = '';
}
/**
* Assigns the role to the user
*
* @return \Illuminate\Http\RedirectResponse
*/
public function save()
{
$this->selectedUser->assignRole($this->role);
return redirect()->to(route('moderator.crew.index'));
}
/**
* Triggers the filtering function if the email/name is being changed
*
* @return void
*/
public function updatedSearchValue() : void
{
$this->users = $this->defaultUsers;
if (!empty($this->searchValue)) {
$this->users = $this->users->filter(function ($user) {
$nameMatch = stripos($user->name, $this->searchValue) !== false;
$emailMatch = stripos($user->email, $this->searchValue) !== false;
return $nameMatch || $emailMatch;
});
}
}
/**
* Triggered when the user chooses the assignee
*
* @param $id
* @return void
*/
public function selectUser($id) : void
{
$this->selectedUser = User::find($id);
$this->searchValue = $this->selectedUser->name;
$this->updatedSearchValue();
$this->isDropdownVisible = false;
}
/**
* Responsible for the visibility status of the dropdown
*
* @return void
*/
public function toggleDropdown() : void
{
$this->isDropdownVisible = !$this->isDropdownVisible;
}
/**
* Renders the component
*
* @return View
*/
public function render() : View
{
return view('livewire.crew.assign-role-to-user');
}
}