Newer
Older

Yassine Doghri
committed
<?php

Yassine Doghri
committed

Yassine Doghri
committed
/**
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace App\Controllers\Admin;

Yassine Doghri
committed
use App\Authorization\GroupModel;
use App\Entities\User;

Yassine Doghri
committed
use App\Models\UserModel;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\HTTP\RedirectResponse;

Yassine Doghri
committed
use Config\Services;

Yassine Doghri
committed
class UserController extends BaseController

Yassine Doghri
committed
{
protected ?User $user;

Yassine Doghri
committed
public function _remap(string $method, string ...$params): mixed

Yassine Doghri
committed
{

Yassine Doghri
committed
if (count($params) === 0) {
return $this->{$method}();

Yassine Doghri
committed
}

Yassine Doghri
committed
if ($this->user = (new UserModel())->find($params[0])) {
return $this->{$method}();

Yassine Doghri
committed
}
throw PageNotFoundException::forPageNotFound();

Yassine Doghri
committed
}
public function list(): string

Yassine Doghri
committed
{
$data = [
'users' => (new UserModel())->findAll(),
];

Yassine Doghri
committed
return view('admin/user/list', $data);
}
public function view(): string
$data = [
'user' => $this->user,
];
replace_breadcrumb_params([
0 => $this->user->username,
]);
return view('admin/user/view', $data);
}
public function create(): string

Yassine Doghri
committed
$data = [
'roles' => (new GroupModel())->getUserRoles(),
];
return view('admin/user/create', $data);
}
public function attemptCreate(): RedirectResponse

Yassine Doghri
committed
{

Yassine Doghri
committed
$userModel = new UserModel();

Yassine Doghri
committed
// Validate here first, since some things,
// like the password, can only be validated properly here.
$rules = array_merge(
$userModel->getValidationRules([
'only' => ['username'],
]),

Yassine Doghri
committed
[
'email' => 'required|valid_email|is_unique[users.email]',
'password' => 'required|strong_password',

Yassine Doghri
committed
],

Yassine Doghri
committed
);
if (! $this->validate($rules)) {
return redirect()
->back()
->withInput()
->with('errors', $this->validator->getErrors());
}
// Save the user
$user = new User($this->request->getPost());

Yassine Doghri
committed
// Activate user
$user->activate();
// Force user to reset his password on first connection

Yassine Doghri
committed
$user->forcePasswordReset();
if (! $userModel->insert($user)) {

Yassine Doghri
committed
return redirect()
->back()
->withInput()

Yassine Doghri
committed
->with('errors', $userModel->errors());

Yassine Doghri
committed
}
// Success!
return redirect()
->with('message', lang('User.messages.createSuccess', [
'username' => $user->username,
]),);

Yassine Doghri
committed
}
public function edit(): string

Yassine Doghri
committed
{
helper('form');
$roles = (new GroupModel())->getUserRoles();
$roleOptions = array_reduce(
$roles,
function ($result, $role) {
$result[$role->name] = lang('User.roles.' . $role->name);
return $result;
},

Yassine Doghri
committed
[],

Yassine Doghri
committed
$data = [
'user' => $this->user,
'roleOptions' => $roleOptions,

Yassine Doghri
committed
];
replace_breadcrumb_params([
0 => $this->user->username,
]);
return view('admin/user/edit', $data);

Yassine Doghri
committed
}
public function attemptEdit(): RedirectResponse

Yassine Doghri
committed
{
$authorize = Services::authorization();
$roles = $this->request->getPost('roles');
$authorize->setUserGroups($this->user->id, $roles);
// Success!
return redirect()
->with('message', lang('User.messages.rolesEditSuccess', [
'username' => $this->user->username,
]),);

Yassine Doghri
committed
}
public function forcePassReset(): RedirectResponse

Yassine Doghri
committed
{

Yassine Doghri
committed
$userModel = new UserModel();

Yassine Doghri
committed
$this->user->forcePasswordReset();

Yassine Doghri
committed
if (! $userModel->update($this->user->id, $this->user)) {

Yassine Doghri
committed
return redirect()
->back()

Yassine Doghri
committed
->with('errors', $userModel->errors());

Yassine Doghri
committed
}
// Success!
return redirect()

Yassine Doghri
committed
->with(
'message',

Yassine Doghri
committed
lang('User.messages.forcePassResetSuccess', [

Yassine Doghri
committed
'username' => $this->user->username,

Yassine Doghri
committed
]),

Yassine Doghri
committed
);

Yassine Doghri
committed
}
public function ban(): RedirectResponse

Yassine Doghri
committed
{

Yassine Doghri
committed
$authorize = Services::authorization();
if ($authorize->inGroup('superadmin', $this->user->id)) {
return redirect()
->back()
->with('errors', [

Yassine Doghri
committed
lang('User.messages.banSuperAdminError', [

Yassine Doghri
committed
'username' => $this->user->username,
]),
]);
}

Yassine Doghri
committed
$userModel = new UserModel();

Yassine Doghri
committed
// TODO: add ban reason?

Yassine Doghri
committed
$this->user->ban('');
if (! $userModel->update($this->user->id, $this->user)) {

Yassine Doghri
committed
return redirect()
->back()

Yassine Doghri
committed
->with('errors', $userModel->errors());

Yassine Doghri
committed
}
return redirect()
->with('message', lang('User.messages.banSuccess', [
'username' => $this->user->username,
]),);

Yassine Doghri
committed
}
public function unBan(): RedirectResponse

Yassine Doghri
committed
{

Yassine Doghri
committed
$userModel = new UserModel();

Yassine Doghri
committed
$this->user->unBan();
if (! $userModel->update($this->user->id, $this->user)) {

Yassine Doghri
committed
return redirect()
->back()

Yassine Doghri
committed
->with('errors', $userModel->errors());

Yassine Doghri
committed
}
return redirect()
->with('message', lang('User.messages.unbanSuccess', [
'username' => $this->user->username,
]),);

Yassine Doghri
committed
}
public function delete(): RedirectResponse

Yassine Doghri
committed
{

Yassine Doghri
committed
$authorize = Services::authorization();
if ($authorize->inGroup('superadmin', $this->user->id)) {
return redirect()
->back()
->with('errors', [

Yassine Doghri
committed
lang('User.messages.deleteSuperAdminError', [

Yassine Doghri
committed
'username' => $this->user->username,
]),
]);
}

Yassine Doghri
committed
(new UserModel())->delete($this->user->id);

Yassine Doghri
committed
return redirect()

Yassine Doghri
committed
->back()
->with('message', lang('User.messages.deleteSuccess', [
'username' => $this->user->username,
]),);