@@ 17-131 (lines=115) @@ | ||
14 | * @author PyroCMS, Inc. <[email protected]> |
|
15 | * @author Ryan Thompson <[email protected]> |
|
16 | */ |
|
17 | class PermissionFormBuilder extends FormBuilder |
|
18 | { |
|
19 | ||
20 | /** |
|
21 | * The addon to modify |
|
22 | * permissions for. |
|
23 | * |
|
24 | * @var null|Addon |
|
25 | */ |
|
26 | protected $addon = null; |
|
27 | ||
28 | /** |
|
29 | * No model needed. |
|
30 | * |
|
31 | * @var bool |
|
32 | */ |
|
33 | protected $model = false; |
|
34 | ||
35 | /** |
|
36 | * Disable saving. |
|
37 | * |
|
38 | * @var bool |
|
39 | */ |
|
40 | protected $save = false; |
|
41 | ||
42 | /** |
|
43 | * The form actions. |
|
44 | * |
|
45 | * @var array |
|
46 | */ |
|
47 | protected $actions = [ |
|
48 | 'save' => [ |
|
49 | 'href' => 'admin/users/permissions/{request.route.parameters.id}', |
|
50 | ], |
|
51 | ]; |
|
52 | ||
53 | /** |
|
54 | * The form options. |
|
55 | * |
|
56 | * @var array |
|
57 | */ |
|
58 | protected $options = [ |
|
59 | 'breadcrumb' => false, |
|
60 | 'permission' => 'anomaly.module.users::users.permissions', |
|
61 | ]; |
|
62 | ||
63 | /** |
|
64 | * Fired when builder is ready to build. |
|
65 | * |
|
66 | * @param RoleRepositoryInterface $roles |
|
67 | * @param BreadcrumbCollection $breadcrumbs |
|
68 | * @param MessageBag $messages |
|
69 | * @param Redirector $redirect |
|
70 | * @return \Illuminate\Http\RedirectResponse |
|
71 | */ |
|
72 | public function onReady( |
|
73 | RoleRepositoryInterface $roles, |
|
74 | BreadcrumbCollection $breadcrumbs, |
|
75 | MessageBag $messages, |
|
76 | Redirector $redirect |
|
77 | ) { |
|
78 | $this->setEntry($role = $roles->find($this->getEntry())); |
|
79 | ||
80 | if ($role->getSlug() === 'admin') { |
|
81 | ||
82 | $messages->warning('anomaly.module.users::warning.modify_admin_role'); |
|
83 | ||
84 | $this->setFormResponse($redirect->to('admin/users/roles')); |
|
85 | ||
86 | return; |
|
87 | } |
|
88 | ||
89 | $breadcrumbs->add($role->getName(), 'admin/users/roles/edit/' . $role->getId()); |
|
90 | $breadcrumbs->add( |
|
91 | 'anomaly.module.users::breadcrumb.permissions', |
|
92 | 'admin/users/roles/permissions/' . $role->getId() |
|
93 | ); |
|
94 | } |
|
95 | ||
96 | /** |
|
97 | * If nothing is posted then |
|
98 | * the role gets no permissions. |
|
99 | * |
|
100 | * @param RoleRepositoryInterface $roles |
|
101 | */ |
|
102 | public function onPost(RoleRepositoryInterface $roles) |
|
103 | { |
|
104 | if (!$this->hasPostData() && $entry = $this->getEntry()) { |
|
105 | $roles->save($entry->setAttribute('permissions', [])); |
|
106 | } |
|
107 | } |
|
108 | ||
109 | /** |
|
110 | * Get the addon. |
|
111 | * |
|
112 | * @return null|Addon |
|
113 | */ |
|
114 | public function getAddon() |
|
115 | { |
|
116 | return $this->addon; |
|
117 | } |
|
118 | ||
119 | /** |
|
120 | * Set the addon. |
|
121 | * |
|
122 | * @param Addon $addon |
|
123 | * @return $this |
|
124 | */ |
|
125 | public function setAddon(Addon $addon) |
|
126 | { |
|
127 | $this->addon = $addon; |
|
128 | ||
129 | return $this; |
|
130 | } |
|
131 | } |
|
132 |
@@ 18-130 (lines=113) @@ | ||
15 | * @author PyroCMS, Inc. <[email protected]> |
|
16 | * @author Ryan Thompson <[email protected]> |
|
17 | */ |
|
18 | class PermissionFormBuilder extends FormBuilder |
|
19 | { |
|
20 | ||
21 | /** |
|
22 | * The addon to modify |
|
23 | * permissions for. |
|
24 | * |
|
25 | * @var null|Addon |
|
26 | */ |
|
27 | protected $addon = null; |
|
28 | ||
29 | /** |
|
30 | * No model needed. |
|
31 | * |
|
32 | * @var bool |
|
33 | */ |
|
34 | protected $model = false; |
|
35 | ||
36 | /** |
|
37 | * Disable saving. |
|
38 | * |
|
39 | * @var bool |
|
40 | */ |
|
41 | protected $save = false; |
|
42 | ||
43 | /** |
|
44 | * The form actions. |
|
45 | * |
|
46 | * @var array |
|
47 | */ |
|
48 | protected $actions = [ |
|
49 | 'save' => [ |
|
50 | 'href' => 'admin/users/permissions/{request.route.parameters.id}', |
|
51 | ], |
|
52 | ]; |
|
53 | ||
54 | /** |
|
55 | * The form options. |
|
56 | * |
|
57 | * @var array |
|
58 | */ |
|
59 | protected $options = [ |
|
60 | 'breadcrumb' => false, |
|
61 | 'permission' => 'anomaly.module.users::users.permissions', |
|
62 | ]; |
|
63 | ||
64 | /** |
|
65 | * Fired when builder is ready to build. |
|
66 | * |
|
67 | * @param UserRepositoryInterface $users |
|
68 | * @param BreadcrumbCollection $breadcrumbs |
|
69 | * @param MessageBag $messages |
|
70 | * @param Redirector $redirect |
|
71 | * @return \Illuminate\Http\RedirectResponse |
|
72 | */ |
|
73 | public function onReady( |
|
74 | UserRepositoryInterface $users, |
|
75 | RoleRepositoryInterface $roles, |
|
76 | BreadcrumbCollection $breadcrumbs, |
|
77 | MessageBag $messages, |
|
78 | Redirector $redirect |
|
79 | ) { |
|
80 | $this->setEntry($user = $users->find($this->getEntry())); |
|
81 | ||
82 | if ($user->hasRole($roles->findBySlug('admin'))) { |
|
83 | ||
84 | $messages->warning('anomaly.module.users::warning.modify_admin_user'); |
|
85 | ||
86 | $this->setFormResponse($redirect->to('admin/users')); |
|
87 | ||
88 | return; |
|
89 | } |
|
90 | ||
91 | $breadcrumbs->add($user->getDisplayName(), 'admin/users/edit/' . $user->getId()); |
|
92 | $breadcrumbs->add('anomaly.module.users::breadcrumb.permissions', 'admin/users/permissions/' . $user->getId()); |
|
93 | } |
|
94 | ||
95 | /** |
|
96 | * If nothing is posted then |
|
97 | * the user gets no permissions. |
|
98 | * |
|
99 | * @param UserRepositoryInterface $users |
|
100 | */ |
|
101 | public function onPost(UserRepositoryInterface $users) |
|
102 | { |
|
103 | if (!$this->hasPostData() && $entry = $this->getEntry()) { |
|
104 | $users->save($entry->setAttribute('permissions', [])); |
|
105 | } |
|
106 | } |
|
107 | ||
108 | /** |
|
109 | * Get the addon. |
|
110 | * |
|
111 | * @return null|Addon |
|
112 | */ |
|
113 | public function getAddon() |
|
114 | { |
|
115 | return $this->addon; |
|
116 | } |
|
117 | ||
118 | /** |
|
119 | * Set the addon. |
|
120 | * |
|
121 | * @param Addon $addon |
|
122 | * @return $this |
|
123 | */ |
|
124 | public function setAddon(Addon $addon) |
|
125 | { |
|
126 | $this->addon = $addon; |
|
127 | ||
128 | return $this; |
|
129 | } |
|
130 | } |
|
131 |