Passed
Branch main (08d999)
by Sammy
03:02
created

OperatorController::modelClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* Welcome to the User controller
4
 *
5
 * The user controller works this way:
6
 * 1. It rubs the lotion on its skin
7
 * 2. It does as its told and it does it whenever its told
8
 * 3. If it doesn't rub the lotion, it gets the hose again
9
 * 4. It puts the lotion in the basket.
10
 * 5. Put the fucking lotion in the basket !
11
 */
12
13
namespace HexMakina\kadro\Controllers;
14
15
use HexMakina\Crudites\Crudites;
16
use HexMakina\kadro\Auth\{Operator,OperatorInterface,ACL,AccessRefusedException};
17
18
class OperatorController extends \HexMakina\kadro\Controllers\ORMController
19
{
20
21
    public function edit()
22
    {
23
        parent::edit();
24
25
      // do we create? or do we edit someone else ? must be admin
26
        if (is_null($this->load_model) || $this->operator()->operator_id() !== $this->load_model->operator_id()) {
27
            $this->authorize('group_admin');
28
        }
29
    }
30
31
    public function modelClassName(): string
32
    {
33
        return "\HexMakina\kadro\Auth\Operator";
34
    }
35
36
    public function save()
37
    {
38
        if ($this->operator()->operator_id() !== $this->form_model->operator_id()) {
39
            $this->authorize('group_admin');
40
        }
41
42
        parent::save();
43
    }
44
45
    public function before_save()
46
    {
47
      //------------------------------------------------------------- PASSWORDS
48
        if ($this->form_model->get('password') != $this->form_model->get('password_verification')) {
49
            $this->add_error('KADRO_operator_ERR_PASSWORDS_MISMATCH');
50
            $this->logger()->warning($this->l('KADRO_operator_ERR_PASSWORDS_MISMATCH'));
51
            $this->edit();
52
        }
53
        return $this->errors(); // useless call, errors are managed globally but interface expects it.. refactor needed
54
    }
55
56
    public function dashboard()
57
    {
58
        $real_operator_class = get_class($this->operator());
59
        $this->viewport('users', $real_operator_class::filter([], ['order_by' => [null,'username', 'ASC']]));
60
    }
61
62
    public function destroy()
63
    {
64
        $this->change_active();
65
    }
66
67
    public function change_active()
68
    {
69
        parent::authorize('group_admin');
70
71
        $operator = Operator::one($this->router()->params());
72
        if ($operator->username() == $this->operator()->username()) {
73
            throw new AccessRefusedException();
74
        }
75
76
        if (Operator::toggle_boolean(Operator::table_name(), 'active', $operator->operator_id()) === true) {
77
            $confirmation_message = $operator->is_active() ? 'KADRO_operator_DISABLED' : 'KADRO_operator_ENABLED';
78
            $this->logger()->nice($this->l($confirmation_message, [$operator->name()]));
79
        } else {
80
            $this->logger()->warning($this->l('CRUDITES_ERR_QUERY_FAILED'));
81
        }
82
83
        $this->router()->hop_back();
84
    }
85
86
    public function change_acl()
87
    {
88
        parent::authorize('group_admin');
89
90
        $operator = Operator::one(['username' => $this->router()->params('username')]);
91
        if ($operator->username() == $this->operator()->username()) {
92
            throw new AccessRefusedException();
93
        }
94
95
        $permission_id = $this->router()->params('permission_id');
96
97
        $row_data = ['operator_id' => $operator->operator_id(), 'permission_id' => $permission_id];
98
        $row = ACL::table()->restore($row_data);
99
        if ($row->is_new()) {
100
            $row = ACL::table()->produce($row_data);
101
            $row->persist();
102
        } else {
103
            $row->wipe();
104
        }
105
      // force reload for permission purposes
106
        $operator = get_class($operator)::one($operator->operator_id());
107
108
        $this->box('OperatorInterface', $operator);
109
        $this->router()->hop_back();
110
    }
111
}
112