2020-07-10 12:20:25 +00:00
|
|
|
<?php
|
2020-08-04 11:25:22 +00:00
|
|
|
|
2020-07-10 12:20:25 +00:00
|
|
|
/**
|
|
|
|
* @copyright 2020 Podlibre
|
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Controllers\Admin;
|
|
|
|
|
2020-07-31 16:05:10 +00:00
|
|
|
use App\Authorization\GroupModel;
|
|
|
|
use App\Models\UserModel;
|
|
|
|
use Config\Services;
|
2020-07-10 12:20:25 +00:00
|
|
|
|
|
|
|
class User extends BaseController
|
|
|
|
{
|
2020-07-31 16:05:10 +00:00
|
|
|
protected ?\App\Entities\User $user;
|
2020-07-10 12:20:25 +00:00
|
|
|
|
|
|
|
public function _remap($method, ...$params)
|
|
|
|
{
|
|
|
|
if (count($params) > 0) {
|
2020-08-04 11:25:22 +00:00
|
|
|
if (!($this->user = (new UserModel())->find($params[0]))) {
|
2020-07-10 12:20:25 +00:00
|
|
|
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->$method();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function list()
|
|
|
|
{
|
2020-08-04 11:25:22 +00:00
|
|
|
$data = ['users' => (new UserModel())->findAll()];
|
2020-07-10 12:20:25 +00:00
|
|
|
|
|
|
|
return view('admin/user/list', $data);
|
|
|
|
}
|
|
|
|
|
2020-08-05 16:10:39 +00:00
|
|
|
public function view()
|
|
|
|
{
|
|
|
|
$data = ['user' => $this->user];
|
|
|
|
|
|
|
|
replace_breadcrumb_params([0 => $this->user->username]);
|
|
|
|
return view('admin/user/view', $data);
|
|
|
|
}
|
|
|
|
|
2020-07-10 12:20:25 +00:00
|
|
|
public function create()
|
2020-07-16 10:08:23 +00:00
|
|
|
{
|
2020-07-31 16:05:10 +00:00
|
|
|
$data = [
|
|
|
|
'roles' => (new GroupModel())->getUserRoles(),
|
|
|
|
];
|
|
|
|
|
2020-08-05 16:10:39 +00:00
|
|
|
return view('admin/user/create', $data);
|
2020-07-16 10:08:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function attemptCreate()
|
2020-07-10 12:20:25 +00:00
|
|
|
{
|
2020-08-04 11:25:22 +00:00
|
|
|
$userModel = new UserModel();
|
2020-07-10 12:20:25 +00:00
|
|
|
|
|
|
|
// Validate here first, since some things,
|
|
|
|
// like the password, can only be validated properly here.
|
|
|
|
$rules = array_merge(
|
2020-08-04 11:25:22 +00:00
|
|
|
$userModel->getValidationRules(['only' => ['username']]),
|
2020-07-10 12:20:25 +00:00
|
|
|
[
|
|
|
|
'email' => 'required|valid_email|is_unique[users.email]',
|
|
|
|
'password' => 'required|strong_password',
|
|
|
|
'pass_confirm' => 'required|matches[password]',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$this->validate($rules)) {
|
2020-07-16 10:08:23 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withInput()
|
|
|
|
->with('errors', $this->validator->getErrors());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the user
|
2020-07-31 16:05:10 +00:00
|
|
|
$user = new \App\Entities\User($this->request->getPost());
|
2020-07-10 12:20:25 +00:00
|
|
|
|
2020-07-16 10:08:23 +00:00
|
|
|
// Activate user
|
|
|
|
$user->activate();
|
|
|
|
|
|
|
|
// Force user to reset his password on first connection
|
2020-07-31 16:05:10 +00:00
|
|
|
$user->forcePasswordReset();
|
2020-07-16 10:08:23 +00:00
|
|
|
|
2020-08-04 11:25:22 +00:00
|
|
|
if (!$userModel->save($user)) {
|
2020-07-10 12:20:25 +00:00
|
|
|
return redirect()
|
2020-07-16 10:08:23 +00:00
|
|
|
->back()
|
|
|
|
->withInput()
|
2020-08-04 11:25:22 +00:00
|
|
|
->with('errors', $userModel->errors());
|
2020-07-10 12:20:25 +00:00
|
|
|
}
|
2020-07-16 10:08:23 +00:00
|
|
|
|
|
|
|
// Success!
|
|
|
|
return redirect()
|
|
|
|
->route('user_list')
|
2020-07-31 16:05:10 +00:00
|
|
|
->with(
|
|
|
|
'message',
|
2020-08-04 11:25:22 +00:00
|
|
|
lang('User.messages.createSuccess', [
|
2020-07-31 16:05:10 +00:00
|
|
|
'username' => $user->username,
|
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function edit()
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'user' => $this->user,
|
|
|
|
'roles' => (new GroupModel())->getUserRoles(),
|
|
|
|
];
|
|
|
|
|
2020-08-05 16:10:39 +00:00
|
|
|
replace_breadcrumb_params([0 => $this->user->username]);
|
|
|
|
return view('admin/user/edit', $data);
|
2020-07-31 16:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function attemptEdit()
|
|
|
|
{
|
|
|
|
$authorize = Services::authorization();
|
|
|
|
|
|
|
|
$roles = $this->request->getPost('roles');
|
|
|
|
$authorize->setUserGroups($this->user->id, $roles);
|
|
|
|
|
|
|
|
// Success!
|
|
|
|
return redirect()
|
|
|
|
->route('user_list')
|
|
|
|
->with(
|
|
|
|
'message',
|
2020-08-04 11:25:22 +00:00
|
|
|
lang('User.messages.rolesEditSuccess', [
|
2020-07-31 16:05:10 +00:00
|
|
|
'username' => $this->user->username,
|
|
|
|
])
|
|
|
|
);
|
2020-07-10 12:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function forcePassReset()
|
|
|
|
{
|
2020-08-04 11:25:22 +00:00
|
|
|
$userModel = new UserModel();
|
2020-07-31 16:05:10 +00:00
|
|
|
$this->user->forcePasswordReset();
|
2020-07-10 12:20:25 +00:00
|
|
|
|
2020-08-04 11:25:22 +00:00
|
|
|
if (!$userModel->save($this->user)) {
|
2020-07-10 12:20:25 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
2020-08-04 11:25:22 +00:00
|
|
|
->with('errors', $userModel->errors());
|
2020-07-10 12:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Success!
|
|
|
|
return redirect()
|
|
|
|
->route('user_list')
|
2020-07-31 16:05:10 +00:00
|
|
|
->with(
|
|
|
|
'message',
|
2020-08-04 11:25:22 +00:00
|
|
|
lang('User.messages.forcePassResetSuccess', [
|
2020-07-31 16:05:10 +00:00
|
|
|
'username' => $this->user->username,
|
|
|
|
])
|
|
|
|
);
|
2020-07-10 12:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function ban()
|
|
|
|
{
|
2020-07-31 16:05:10 +00:00
|
|
|
$authorize = Services::authorization();
|
|
|
|
if ($authorize->inGroup('superadmin', $this->user->id)) {
|
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->with('errors', [
|
2020-08-04 11:25:22 +00:00
|
|
|
lang('User.messages.banSuperAdminError', [
|
2020-07-31 16:05:10 +00:00
|
|
|
'username' => $this->user->username,
|
|
|
|
]),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-08-04 11:25:22 +00:00
|
|
|
$userModel = new UserModel();
|
2020-07-31 16:05:10 +00:00
|
|
|
// TODO: add ban reason?
|
2020-07-10 12:20:25 +00:00
|
|
|
$this->user->ban('');
|
|
|
|
|
2020-08-04 11:25:22 +00:00
|
|
|
if (!$userModel->save($this->user)) {
|
2020-07-10 12:20:25 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
2020-08-04 11:25:22 +00:00
|
|
|
->with('errors', $userModel->errors());
|
2020-07-10 12:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()
|
|
|
|
->route('user_list')
|
2020-07-31 16:05:10 +00:00
|
|
|
->with(
|
|
|
|
'message',
|
2020-08-04 11:25:22 +00:00
|
|
|
lang('User.messages.banSuccess', [
|
2020-07-31 16:05:10 +00:00
|
|
|
'username' => $this->user->username,
|
|
|
|
])
|
|
|
|
);
|
2020-07-10 12:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function unBan()
|
|
|
|
{
|
2020-08-04 11:25:22 +00:00
|
|
|
$userModel = new UserModel();
|
2020-07-10 12:20:25 +00:00
|
|
|
$this->user->unBan();
|
|
|
|
|
2020-08-04 11:25:22 +00:00
|
|
|
if (!$userModel->save($this->user)) {
|
2020-07-10 12:20:25 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
2020-08-04 11:25:22 +00:00
|
|
|
->with('errors', $userModel->errors());
|
2020-07-10 12:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()
|
|
|
|
->route('user_list')
|
2020-07-31 16:05:10 +00:00
|
|
|
->with(
|
|
|
|
'message',
|
2020-08-04 11:25:22 +00:00
|
|
|
lang('User.messages.unbanSuccess', [
|
2020-07-31 16:05:10 +00:00
|
|
|
'username' => $this->user->username,
|
|
|
|
])
|
|
|
|
);
|
2020-07-10 12:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function delete()
|
|
|
|
{
|
2020-07-31 16:05:10 +00:00
|
|
|
$authorize = Services::authorization();
|
|
|
|
if ($authorize->inGroup('superadmin', $this->user->id)) {
|
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->with('errors', [
|
2020-08-04 11:25:22 +00:00
|
|
|
lang('User.messages.deleteSuperAdminError', [
|
2020-07-31 16:05:10 +00:00
|
|
|
'username' => $this->user->username,
|
|
|
|
]),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-08-04 11:25:22 +00:00
|
|
|
(new UserModel())->delete($this->user->id);
|
2020-07-10 12:20:25 +00:00
|
|
|
|
|
|
|
return redirect()
|
2020-07-31 16:05:10 +00:00
|
|
|
->back()
|
|
|
|
->with(
|
|
|
|
'message',
|
2020-08-04 11:25:22 +00:00
|
|
|
lang('User.messages.deleteSuccess', [
|
2020-07-31 16:05:10 +00:00
|
|
|
'username' => $this->user->username,
|
|
|
|
])
|
|
|
|
);
|
2020-07-10 12:20:25 +00:00
|
|
|
}
|
|
|
|
}
|