diff --git a/app/Controllers/Admin/Groups.php b/app/Controllers/Admin/Groups.php index 4689494..303484b 100644 --- a/app/Controllers/Admin/Groups.php +++ b/app/Controllers/Admin/Groups.php @@ -64,8 +64,10 @@ class Groups extends Controller */ public function new() { + $data['groups'] = $this->aauth->listGroups(); + $data['perms'] = $this->aauth->listPerms(); echo view('Templates/HeaderAdmin'); - echo view('Admin/Groups/New'); + echo view('Admin/Groups/New', $data); echo view('Templates/FooterAdmin'); } @@ -78,12 +80,30 @@ class Groups extends Controller { $name = $this->request->getPost('name'); $definition = $this->request->getPost('definition'); + $subGroups = $this->request->getPost('sub_groups'); + $perms = $this->request->getPost('perms'); - if (! $this->aauth->createGroup($name, $definition)) + if (! $groupId = $this->aauth->createGroup($name, $definition)) { return redirect()->back()->with('errors', $this->aauth->getErrorsArray()); } + foreach ($subGroups as $subgroupId => $state) + { + if ($state === 1) + { + $this->aauth->addSubgroup($groupId, $subgroupId); + } + } + + foreach ($perms as $permId => $state) + { + if ($state === 1) + { + $this->aauth->allowGroup($permId, $groupId); + } + } + return redirect()->to('/admin/groups'); } @@ -94,7 +114,11 @@ class Groups extends Controller */ public function edit($groupId) { - $data['group'] = $this->aauth->getGroup($groupId); + $data['group'] = $this->aauth->getGroup($groupId); + $data['groups'] = $this->aauth->listGroups(); + $data['perms'] = $this->aauth->listPerms(); + $data['activeGroups'] = $this->aauth->getSubgroups($groupId); + $data['activePerms'] = $this->aauth->getGroupPerms($groupId); echo view('Templates/HeaderAdmin'); echo view('Admin/Groups/Edit', $data); @@ -110,12 +134,41 @@ class Groups extends Controller { $name = $this->request->getPost('name'); $definition = $this->request->getPost('definition'); + $subGroups = $this->request->getPost('sub_groups'); + $perms = $this->request->getPost('perms'); if (! $this->aauth->updateGroup($groupId, empty($name) ? null : $name, empty($definition) ? null : $definition)) { return redirect()->back()->with('errors', $this->aauth->getErrorsArray()); } + $activeSubGroups = $this->aauth->getSubgroups($groupId); + $activePerms = $this->aauth->getGroupPerms($groupId, 1); + + foreach ($subGroups as $subgroupId => $state) + { + if (! in_array(['subgroup_id' => $subgroupId], $activeSubGroups) && $state === 1) + { + $this->aauth->addSubgroup($groupId, $subgroupId); + } + else if (in_array(['subgroup_id' => $subgroupId], $activeSubGroups) && $state === 0) + { + $this->aauth->removeSubgroup($groupId, $subgroupId); + } + } + + foreach ($perms as $permId => $state) + { + if (! in_array(['perm_id' => $permId], $activePerms) && $state === 1) + { + $this->aauth->allowGroup($permId, $groupId); + } + else if (! in_array(['perm_id' => $permId], $activePerms) && $state === 0) + { + $this->aauth->denyGroup($permId, $groupId); + } + } + return redirect()->to('/admin/groups/edit/' . $groupId); } @@ -126,7 +179,11 @@ class Groups extends Controller */ public function show($groupId) { - $data['group'] = $this->aauth->getGroup($groupId); + $data['group'] = $this->aauth->getGroup($groupId); + $data['groups'] = $this->aauth->listGroups(); + $data['perms'] = $this->aauth->listPerms(); + $data['activeGroups'] = $this->aauth->getSubgroups($groupId); + $data['activePerms'] = $this->aauth->getGroupPerms($groupId); echo view('Templates/HeaderAdmin'); echo view('Admin/Groups/Show', $data); @@ -154,7 +211,11 @@ class Groups extends Controller } } - $data['group'] = $this->aauth->getGroup($groupId); + $data['group'] = $this->aauth->getGroup($groupId); + $data['groups'] = $this->aauth->listGroups(); + $data['perms'] = $this->aauth->listPerms(); + $data['activeGroups'] = $this->aauth->getSubgroups($groupId); + $data['activePerms'] = $this->aauth->getGroupPerms($groupId); echo view('Templates/HeaderAdmin'); echo view('Admin/Groups/Delete', $data); diff --git a/app/Controllers/Admin/Perms.php b/app/Controllers/Admin/Perms.php index c94023b..33f22cb 100644 --- a/app/Controllers/Admin/Perms.php +++ b/app/Controllers/Admin/Perms.php @@ -49,7 +49,7 @@ class Perms extends Controller $data = $this->aauth->listPermsPaginated(); $data['cssFiles'] = [ - '/assets/css/admin/groups/index.css' + '/assets/css/admin/perms/index.css' ]; echo view('Templates/HeaderAdmin', $data); @@ -84,7 +84,7 @@ class Perms extends Controller return redirect()->back()->with('errors', $this->aauth->getErrorsArray()); } - return redirect()->to('/admin/groups'); + return redirect()->to('/admin/perms'); } /** @@ -94,7 +94,7 @@ class Perms extends Controller */ public function edit($permId) { - $data['group'] = $this->aauth->getPerm($permId); + $data['perm'] = $this->aauth->getPerm($permId); echo view('Templates/HeaderAdmin'); echo view('Admin/Perms/Edit', $data); @@ -116,7 +116,7 @@ class Perms extends Controller return redirect()->back()->with('errors', $this->aauth->getErrorsArray()); } - return redirect()->to('/admin/groups/edit/' . $permId); + return redirect()->to('/admin/perms/edit/' . $permId); } /** @@ -126,7 +126,7 @@ class Perms extends Controller */ public function show($permId) { - $data['group'] = $this->aauth->getPerm($permId); + $data['perm'] = $this->aauth->getPerm($permId); echo view('Templates/HeaderAdmin'); echo view('Admin/Perms/Show', $data); @@ -142,7 +142,7 @@ class Perms extends Controller { if (! $this->aauth->getPerm($permId)) { - return redirect()->to('/admin/groups'); + return redirect()->to('/admin/perms'); } $id = $this->request->getPost('id'); @@ -150,11 +150,11 @@ class Perms extends Controller { if ($this->aauth->deletePerm($permId)) { - return redirect()->to('/admin/groups'); + return redirect()->to('/admin/perms'); } } - $data['group'] = $this->aauth->getPerm($permId); + $data['perm'] = $this->aauth->getPerm($permId); echo view('Templates/HeaderAdmin'); echo view('Admin/Perms/Delete', $data); diff --git a/app/Controllers/Admin/Users.php b/app/Controllers/Admin/Users.php index 8851760..5068e80 100644 --- a/app/Controllers/Admin/Users.php +++ b/app/Controllers/Admin/Users.php @@ -67,6 +67,8 @@ class Users extends Controller public function new() { $data['useUsername'] = $this->config->loginUseUsername; + $data['groups'] = $this->aauth->listGroups(); + $data['perms'] = $this->aauth->listPerms(); echo view('Templates/HeaderAdmin'); echo view('Admin/Users/New', $data); @@ -101,6 +103,8 @@ class Users extends Controller { $data['useUsername'] = $this->config->loginUseUsername; $data['user'] = $this->aauth->getUser($userId); + $data['groups'] = $this->aauth->listGroups(); + $data['perms'] = $this->aauth->listPerms(); echo view('Templates/HeaderAdmin'); echo view('Admin/Users/Edit', $data); @@ -117,12 +121,46 @@ class Users extends Controller $email = $this->request->getPost('email'); $username = $this->request->getPost('username'); $password = $this->request->getPost('password'); + $groups = $this->request->getPost('groups'); + $perms = $this->request->getPost('perms'); if (! $this->aauth->updateUser($userId, empty($email) ? null : $email, empty($password) ? null : $password, empty($username) ? null : $username)) { return redirect()->back()->with('errors', $this->aauth->getErrorsArray()); } + $activeGroups = $this->aauth->getUserGroups($userId); + $activePerms = $this->aauth->getUserPerms($userId, 1); + + foreach ($groups as $groupId => $state) + { + if ($groupId === 2) + { + continue; + } + + if (! in_array(['group_id' => $groupId], $activeGroups) && $state === 1) + { + $this->aauth->addMember($groupId, $userId); + } + else if (in_array(['group_id' => $groupId], $activeGroups) && $state === 0) + { + $this->aauth->removeMember($groupId, $userId); + } + } + + foreach ($perms as $permId => $state) + { + if (! in_array(['perm_id' => $permId], $activePerms) && $state === 1) + { + $this->aauth->allowUser($permId, $userId); + } + else if (in_array(['perm_id' => $permId], $activePerms) && $state === 0) + { + $this->aauth->denyUser($permId, $userId); + } + } + return redirect()->to('/admin/users/edit/' . $userId); } @@ -133,7 +171,9 @@ class Users extends Controller */ public function show($userId) { - $data['user'] = $this->aauth->getUser($userId); + $data['user'] = $this->aauth->getUser($userId); + $data['groups'] = $this->aauth->listGroups(); + $data['perms'] = $this->aauth->listPerms(); echo view('Templates/HeaderAdmin'); echo view('Admin/Users/Show', $data); @@ -161,7 +201,9 @@ class Users extends Controller } } - $data['user'] = $this->aauth->getUser($userId); + $data['user'] = $this->aauth->getUser($userId); + $data['groups'] = $this->aauth->listGroups(); + $data['perms'] = $this->aauth->listPerms(); echo view('Templates/HeaderAdmin'); echo view('Admin/Users/Delete', $data); diff --git a/app/Helpers/aauth_helper.php b/app/Helpers/aauth_helper.php index e467f25..adbb745 100644 --- a/app/Helpers/aauth_helper.php +++ b/app/Helpers/aauth_helper.php @@ -35,3 +35,87 @@ if (! function_exists('is_loggedin')) return $aauth->isLoggedIn(); } } + +if (! function_exists('is_member')) +{ + /** + * Is member + * + * @return boolean + */ + function is_member($groupPar, $userId) + { + $aauth = new Aauth(); + return $aauth->isMember($groupPar, $userId); + } +} + +if (! function_exists('is_allowed')) +{ + /** + * Is allowed + * + * @return boolean + */ + function is_allowed($permPar, $userId) + { + $aauth = new Aauth(); + return $aauth->isAllowed($permPar, $userId); + } +} + +if (! function_exists('get_subgroups')) +{ + /** + * Get Sub-Groups by Group Name/Id + * + * @return array + */ + function get_subgroups($groupPar) + { + $aauth = new Aauth(); + return $aauth->getSubgroups($groupPar); + } +} + +if (! function_exists('get_user_perms')) +{ + /** + * Get User Groups + * + * @return array + */ + function get_user_groups($userId) + { + $aauth = new Aauth(); + return $aauth->getUserGroups($userId); + } +} + +if (! function_exists('get_user_perms')) +{ + /** + * Get Group Perms by Group Name/Id + * + * @return array + */ + function get_user_perms($userId, $state = null) + { + $aauth = new Aauth(); + return $aauth->getUserPerms($userId, $state); + } +} + +if (! function_exists('get_group_perms')) +{ + /** + * Get Group Perms by Group Name/Id + * + * @return array + */ + function get_group_perms($groupPar, $state = null) + { + $aauth = new Aauth(); + return $aauth->getGroupPerms($groupPar, $state); + } +} diff --git a/app/Language/en/Admin.php b/app/Language/en/Admin.php index 7bce5cd..95e979a 100644 --- a/app/Language/en/Admin.php +++ b/app/Language/en/Admin.php @@ -50,6 +50,8 @@ return [ 'usersLabelUsernameCurrent' => 'Current username', 'usersLabelPassword' => 'Password', 'usersLabelBanned' => 'Banned', + 'usersLabelGroups' => 'Groups', + 'usersLabelPerms' => 'Perms', 'usersLabelCreatedAt' => 'Created at', 'usersLabelUpdatedAt' => 'Updated at', 'usersLabelLastIPAddress' => 'Last IP-Address', @@ -70,6 +72,8 @@ return [ 'groupsLabelId' => 'Id', 'groupsLabelName' => 'Name', 'groupsLabelDefinition' => 'Definition', + 'groupsLabelSubGroups' => 'Sub-Groups', + 'groupsLabelPerms' => 'Perms', 'groupsLabelNameCurrent' => 'Current Name', 'groupsLabelDefinitionCurrent' => 'Current Definition', 'groupsLabelCreatedAt' => 'Created at', diff --git a/app/Libraries/Aauth.php b/app/Libraries/Aauth.php index cddea0b..9e8d16b 100644 --- a/app/Libraries/Aauth.php +++ b/app/Libraries/Aauth.php @@ -669,7 +669,7 @@ class Aauth } else if (is_null($email) && is_null($password) && is_null($username)) { - return false; + return true; } $data['id'] = $userId; @@ -1317,7 +1317,7 @@ class Aauth if (is_null($name) && is_null($definition)) { - return false; + return true; } else if (! $groupId = $this->getGroupId($groupPar)) { @@ -1444,11 +1444,53 @@ class Aauth return $groupToUserModel->delete($groupId, $userId); } + /** + * Get User Groups + * + * @param integer|string $userId User id + * + * @return object Array of group_id's + */ + public function getUserGroups($userId) + { + $userModel = new UserModel(); + + if (! $userModel->existsById($userId)) + { + return false; + } + + $groupToUserModel = new GroupToUserModel(); + + return $groupToUserModel->findAllByUserId($userId); + } + + /** + * Get User Perms + * + * @param integer|string $userId User id + * + * @return object Array of perm_id's + */ + public function getUserPerms($userId, $state = null) + { + $userModel = new UserModel(); + + if (! $userModel->existsById($userId)) + { + return false; + } + + $permToUserModel = new PermToUserModel(); + + return $permToUserModel->findAllByUserId($userId, $state); + } + /** * Add subgroup to group * - * @param integer $userId User id to add to group - * @param integer|string $groupPar Group id or name to add user to + * @param integer|string $groupPar Group id + * @param integer|string $subgroupPar Subgroup id or name to add to group * * @return boolean Add success/failure */ @@ -1469,6 +1511,10 @@ class Aauth return false; } + else if (! $groupId = $subgroupId) + { + return false; + } else if ($groupToGroupModel->exists($groupId, $subgroupId)) { $this->info(lang('Aauth.alreadyMemberSubgroup')); @@ -1539,6 +1585,26 @@ class Aauth return $groupToGroupModel->findAllByGroupId($groupId); } + /** + * Get group perms + * + * @param integer|string $groupPar Group id or name to get + * @param integer $state State (1 = allowed, 0 = denied) + * + * @return object Array of subgroup_id's + */ + public function getGroupPerms($groupPar, int $state = null) + { + if (! $groupId = $this->getGroupId($groupPar)) + { + return false; + } + + $permToGroupModel = new PermToGroupModel(); + + return $permToGroupModel->findAllByGroupId($groupId, $state); + } + /** * Remove member from all groups * @@ -1907,7 +1973,7 @@ class Aauth if (is_null($name) && is_null($definition)) { - return false; + return true; } else if (! $permId = $this->getPermId($permPar)) { diff --git a/app/Models/Aauth/PermToGroupModel.php b/app/Models/Aauth/PermToGroupModel.php index 338ded5..348f2ec 100644 --- a/app/Models/Aauth/PermToGroupModel.php +++ b/app/Models/Aauth/PermToGroupModel.php @@ -97,12 +97,20 @@ class PermToGroupModel * * @return array|null */ - public function findAllByGroupId(int $groupId) + public function findAllByGroupId(int $groupId, int $state = null) { $builder = $this->builder(); - $builder->select('perm_id, state'); $builder->where('group_id', $groupId); + if (! $state) + { + $builder->select('perm_id, state'); + } + else if ($state) + { + $builder->select('perm_id'); + $builder->where('state', $state); + } return $builder->get()->getResult('array'); } diff --git a/app/Models/Aauth/PermToUserModel.php b/app/Models/Aauth/PermToUserModel.php index 3504c18..9a88c16 100644 --- a/app/Models/Aauth/PermToUserModel.php +++ b/app/Models/Aauth/PermToUserModel.php @@ -97,12 +97,21 @@ class PermToUserModel * * @return array|null */ - public function findAllByUserId(int $userId) + public function findAllByUserId(int $userId, int $state = null) { $builder = $this->builder(); - $builder->select('perm_id, state'); $builder->where('user_id', $userId); + if (! $state) + { + $builder->select('perm_id, state'); + } + else if ($state) + { + $builder->select('perm_id'); + $builder->where('state', $state); + } + return $builder->get()->getResult('array'); } diff --git a/app/Models/Aauth/UserModel.php b/app/Models/Aauth/UserModel.php index 32ee44d..1847fbe 100644 --- a/app/Models/Aauth/UserModel.php +++ b/app/Models/Aauth/UserModel.php @@ -143,9 +143,11 @@ class UserModel extends Model public function updateLastLogin(int $userId) { $builder = $this->builder(); + $request = \Config\Services::request(); - $data['last_login'] = $this->setDate(); - $data['last_activity'] = $this->setDate(); + $data['last_login'] = $this->setDate(); + $data['last_activity'] = $this->setDate(); + $data['last_ip_address'] = $request->getIPAddress(); return $builder->update($data, [$this->primaryKey => $userId]); } diff --git a/app/Views/Admin/Groups/Delete.php b/app/Views/Admin/Groups/Delete.php index aaf404d..ee8a163 100644 --- a/app/Views/Admin/Groups/Delete.php +++ b/app/Views/Admin/Groups/Delete.php @@ -25,6 +25,40 @@

+
+
+
+
+ + + $group['id']], $activeGroups)): ?> + + +
+ +
+ +
+
+
+
+ + + $perm['id'], 'state' => 1], $activePerms)): ?> + + +
+ +
+ +
+
+
+
+
+
+
+ + + +
+ +
+ +
+
+
+
+ + + +
+ +
+ +
+
+