Skip to content
Snippets Groups Projects
UserController.php 6.45 KiB
Newer Older
/**
 * @copyright  2020 Podlibre
 * @license    https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
 * @link       https://castopod.org/
 */

namespace App\Controllers\Admin;

use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\HTTP\RedirectResponse;
class UserController extends BaseController
    public function _remap(string $method, string ...$params): mixed
        if ($this->user = (new UserModel())->find($params[0])) {
    public function list(): string
        $data = [
            'users' => (new UserModel())->findAll(),
        ];
    public function view(): string
        replace_breadcrumb_params([
            0 => $this->user->username,
        ]);
        return view('admin/user/view', $data);
    }

    public function create(): string
        return view('admin/user/create', $data);
    public function attemptCreate(): RedirectResponse

        // Validate here first, since some things,
        // like the password, can only be validated properly here.
        $rules = array_merge(
            $userModel->getValidationRules([
                'only' => ['username'],
            ]),
            [
                'email' => 'required|valid_email|is_unique[users.email]',
                'password' => 'required|strong_password',
            return redirect()
                ->back()
                ->withInput()
                ->with('errors', $this->validator->getErrors());
        }

        // Save the user
        $user = new User($this->request->getPost());
        // Activate user
        $user->activate();

        // Force user to reset his password on first connection
            ->route('user-list')
            ->with('message', lang('User.messages.createSuccess', [
                'username' => $user->username,
            ]),);
    public function edit(): string
        helper('form');

        $roles = (new GroupModel())->getUserRoles();
        $roleOptions = array_reduce(
            $roles,
            function ($result, $role) {
                $result[$role->name] = lang('User.roles.' . $role->name);
                return $result;
            },
            'roleOptions' => $roleOptions,
        replace_breadcrumb_params([
            0 => $this->user->username,
        ]);
        return view('admin/user/edit', $data);
    public function attemptEdit(): RedirectResponse
    {
        $authorize = Services::authorization();

        $roles = $this->request->getPost('roles');
        $authorize->setUserGroups($this->user->id, $roles);

        // Success!
        return redirect()
            ->route('user-list')
            ->with('message', lang('User.messages.rolesEditSuccess', [
                'username' => $this->user->username,
            ]),);
    public function forcePassReset(): RedirectResponse
        if (! $userModel->update($this->user->id, $this->user)) {
            ->route('user-list')
                lang('User.messages.forcePassResetSuccess', [
    public function ban(): RedirectResponse
        $authorize = Services::authorization();
        if ($authorize->inGroup('superadmin', $this->user->id)) {
            return redirect()
                ->back()
                ->with('errors', [
                    lang('User.messages.banSuperAdminError', [
        if (! $userModel->update($this->user->id, $this->user)) {
            ->route('user-list')
            ->with('message', lang('User.messages.banSuccess', [
                'username' => $this->user->username,
            ]),);
    public function unBan(): RedirectResponse
        if (! $userModel->update($this->user->id, $this->user)) {
            ->route('user-list')
            ->with('message', lang('User.messages.unbanSuccess', [
                'username' => $this->user->username,
            ]),);
    public function delete(): RedirectResponse
        $authorize = Services::authorization();
        if ($authorize->inGroup('superadmin', $this->user->id)) {
            return redirect()
                ->back()
                ->with('errors', [
                    lang('User.messages.deleteSuperAdminError', [
        (new UserModel())->delete($this->user->id);
            ->with('message', lang('User.messages.deleteSuccess', [
                'username' => $this->user->username,
            ]),);