Newer
Older

Yassine Doghri
committed
<?php
/**
* @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\Models\UserModel;
use Config\Services;

Yassine Doghri
committed
class User extends BaseController
{

Yassine Doghri
committed
protected ?\App\Entities\User $user;

Yassine Doghri
committed
public function _remap($method, ...$params)
{
if (count($params) > 0) {
$user_model = new UserModel();
if (!($this->user = $user_model->find($params[0]))) {

Yassine Doghri
committed
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
}
}
return $this->$method();
}
public function list()
{

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

Yassine Doghri
committed
return view('admin/user/list', $data);
}
public function create()

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

Yassine Doghri
committed
{
$user_model = new UserModel();
// Validate here first, since some things,
// like the password, can only be validated properly here.
$rules = array_merge(
$user_model->getValidationRules(['only' => ['username']]),
[
'email' => 'required|valid_email|is_unique[users.email]',
'password' => 'required|strong_password',
'pass_confirm' => 'required|matches[password]',
]
);
if (!$this->validate($rules)) {
return redirect()
->back()
->withInput()
->with('errors', $this->validator->getErrors());
}
// Save the user

Yassine Doghri
committed
$user = new \App\Entities\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 (!$user_model->save($user)) {

Yassine Doghri
committed
return redirect()
->back()
->withInput()
->with('errors', $user_model->errors());

Yassine Doghri
committed
}
// Success!
return redirect()
->route('user_list')

Yassine Doghri
committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
->with(
'message',
lang('User.createSuccess', [
'username' => $user->username,
])
);
}
public function edit()
{
$data = [
'user' => $this->user,
'roles' => (new GroupModel())->getUserRoles(),
];
echo view('admin/user/edit', $data);
}
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',
lang('User.rolesEditSuccess', [
'username' => $this->user->username,
])
);

Yassine Doghri
committed
}
public function forcePassReset()
{
$user_model = new UserModel();

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

Yassine Doghri
committed
if (!$user_model->save($this->user)) {
return redirect()
->back()
->with('errors', $user_model->errors());
}
// Success!
return redirect()
->route('user_list')

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

Yassine Doghri
committed
}
public function ban()
{

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

Yassine Doghri
committed
$user_model = new UserModel();

Yassine Doghri
committed
// TODO: add ban reason?

Yassine Doghri
committed
$this->user->ban('');
if (!$user_model->save($this->user)) {
return redirect()
->back()
->with('errors', $user_model->errors());
}
return redirect()
->route('user_list')

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

Yassine Doghri
committed
}
public function unBan()
{
$user_model = new UserModel();
$this->user->unBan();
if (!$user_model->save($this->user)) {
return redirect()
->back()
->with('errors', $user_model->errors());
}
return redirect()
->route('user_list')

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

Yassine Doghri
committed
}
public function delete()
{

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

Yassine Doghri
committed
$user_model = new UserModel();
$user_model->delete($this->user->id);
return redirect()

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