/var/www/hkosl.com/nick/codeigniter/application/controllers/webadmin/Bk_sys_user.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

use 
Illuminate\Database\Capsule\Manager as DB;

class 
Bk_sys_user extends CI_Controller
{

    public function 
__construct()
    {
        
parent::__construct();

        
$this->load->model('Sys_user_model');
        
$this->load->model('Phprbac_roles_model');

        
//load decryption
        
$this->load->helper('cryptojs-aes');
    }

    public function 
create()
    {
        
check_sys_user_login(['create_sys_user']);

        
$sys_user_role = [];
        
$result        Phprbac_roles_model::where('Level''>'$_SESSION['role_level'])->orderBy('Level''ASC')->get();
        foreach (
$result as $role) {
            
$sys_user_role[$role['Title']] = __($role['Description']);
        }

        
$data['sys_user_role_list'] = $sys_user_role;

        
$this->load->view('webadmin/sys_user_form'$data);
    }

    public function 
delete($id)
    {
        
check_sys_user_login(['delete_sys_user']);

        
$result Sys_user_model::find($id);

        
$this->role_level_checking($result);

        
$data = array(
            
"deleted"    => 1,
            
"deleted_by" => $_SESSION['sys_user_id'],
            
"deleted_at" => date('Y-m-d H:i:s'),
        );
        
Sys_user_model::where('id'$id)->update($data);

        
$_SESSION['success_msg'] = __('Deleted successfully');

        
redirect(admin_url('bk_permission/delete_user/' $result->login_role '/' $id));
    }

    public function 
index()
    {
        
check_sys_user_login(['view_sys_user']);

        
$data["sys_user_index"] = Sys_user_model::Where('role_level''>'$_SESSION['role_level'])->orWhere(function ($query) {
            
$query
                
->where('role_level''='$_SESSION['role_level'])
                ->
where('id''='$_SESSION['sys_user_id']);
        })->
where('id''!='$_SESSION["sys_user_id"])->get();

        
$this->load->view('webadmin/sys_user_index'$data);
    }

    public function 
modify($id)
    {
        
check_sys_user_login(['update_sys_user']);

        
$data Sys_user_model::find($id);

        
$this->role_level_checking($data);

        if (empty(
$data)) {
            
redirect(admin_url('bk_sys_user'));
        } else {
            
$data $data->toArray();
        }

        
$data["id"] = $id;

        
$sys_user_role = [];
        
$operation '>';
        if (
$id == $_SESSION['sys_user_id']) {
            
$operation '>=';
        }

        
$result Phprbac_roles_model::where('Level'$operation$_SESSION['role_level'])->orderBy('Level''ASC')->get();
        foreach (
$result as $role) {
            
$sys_user_role[$role['Title']] = __($role['Description']);
        }

        
$data['sys_user_role_list'] = $sys_user_role;

        
$this->load->view('webadmin/sys_user_form'$data);
    }

    public function 
role_level_checking($data)
    {
        if (empty(
$data)) {
            
//error
            
$_SESSION['error_msg'] = __('Cannot find valid record.');
            
redirect(admin_url('bk_sys_user'));
        } else {
            if (
$data['role_level'] < $_SESSION['role_level'] || ($data['role_level'] == $_SESSION['role_level'] && $data['id'] != $_SESSION['sys_user_id'])) {
                
//error
                
$_SESSION['error_msg'] = __('You do not have permission.');
                
redirect(admin_url('bk_sys_user'));
            }
        }
    }

    public function 
status($id$status)
    {
        
check_sys_user_login(['update_sys_user']);

        
$result Sys_user_model::find($id);

        
$this->role_level_checking($result);

        
$data = array(
            
"status"     => $status,
            
'updated_by' => $_SESSION["sys_user_id"],
        );
        
Sys_user_model::where('id'$id)->update($data);

        
$_SESSION['success_msg'] = __('Update Successfully.');

        
redirect(admin_url('bk_sys_user'));
    }

    public function 
submit_form($id null)
    {
        
check_sys_user_login(['create_sys_user''update_sys_user']);

        
//tackle language parameter
        
if (!is_numeric($id)) {
            
$id null;
        }

        
//decryption
        
$password $this->input->post('password') ? cryptoJsAesDecrypt($this->input->post('password')) : '';
        
$password2 $this->input->post('password2') ? cryptoJsAesDecrypt($this->input->post('password2')) : '';

        
//set form data
        
$this->form_validation->set_data(array(
            
'login_role' => $this->input->post('login_role'),
            
'login_id'   => $this->input->post('login_id'),
            
'login_name' => $this->input->post('login_name'),
            
'email'      => $this->input->post('email'),
            
'password'   => $password,
            
'password2'  => $password2,
        ));

        
//validate data
        
$this->form_validation->set_rules('login_role'__('Login Role'), 'trim|required', array(
            
'required' => __('Please enter') . ' %s.',
        ));
        
$this->form_validation->set_rules('login_id'__('Login ID'), 'trim|required', array(
            
'required' => __('Please enter') . ' %s.',
        ));
        
$this->form_validation->set_rules('login_name'__('Login Name'), 'trim|required', array(
            
'required' => __('Please enter') . ' %s.',
        ));

        
$this->form_validation->set_rules('password'__('Login Password'), 'trim|regex_match[/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9]).{8,}$/]', array(
            
'required'    => __('Please enter') . ' %s.',
            
'regex_match' => '%s ' __('should contain at least 8 characters, at least 1 upper letter, at least 1 lower letter and at least 1 number.'),
        ));

        
$this->form_validation->set_rules('password2'__('Confirm Password'), 'trim|matches[password]', array(
            
'required' => __('Please enter') . ' %s.',
            
//'regex_match' => '%s ' . __('should contain at least 8 characters, at least 1 upper letter, at least 1 lower letter and at least 1 number.'),
        
));

        if (!empty(
$this->input->post('login_role'))) {
            
//check role level
            
$role Phprbac_roles_model::where('Title'$this->input->post('login_role'))->first();

            if (empty(
$id)) {
                
//only allow to create lower level user
                
if ($role['Level'] <= $_SESSION['role_level']) {
                    
$_SESSION['error_msg'] = __('You do not have permission.');
                }
            } else {
                
//cannot update upper level user and
                
if ($role['Level'] < $_SESSION['role_level'] || ($role['Level'] == $_SESSION['role_level'] && $id != $_SESSION['sys_user_id'])) {
                    
$_SESSION['error_msg'] = __('You do not have permission.');
                }
            }
        }

        if (
$this->form_validation->run() == false || !empty($_SESSION['error_msg'])) {
            if (empty(
$id)) {
                
$this->create();
            } else {
                
$this->modify($id);
            }
        } else {

            
//form data
            
$data = array(
                
'login_role' => $this->input->post('login_role'),
                
'role_level' => $role['Level'],
                
'login_id'   => $this->input->post('login_id'),
                
'login_name' => $this->input->post('login_name'),
                
'updated_by' => $_SESSION["sys_user_id"],
            );


            if (
$password) {
                
$data['login_pw'] = sha256_hash($password);
            }

            
//insert into database
            
if (empty($id)) {
                
validate_user_access(['create_sys_user'], 0);

                
$data['created_by'] = $_SESSION["sys_user_id"];
                
//Sys_user_model::create($data);
                
$sys_user = new Sys_user_model();
                
$sys_user->fill($data)->save();

                
$id $sys_user->id;

                
$_SESSION['success_msg'] = __('Create Successfully.');

                
redirect(admin_url('bk_permission/new_user/' $this->input->post('login_role') . '/' $id));

            } else {
                
validate_user_access(['update_sys_user'], 0);
                
$old_sys_user Sys_user_model::find($id);
                
Sys_user_model::where('id'$id)->update($data);

                
$_SESSION['success_msg'] = __('Update Successfully.');


                if (
$old_sys_user->id && $old_sys_user->login_role <> $this->input->post('login_role')) {
                    
redirect(admin_url('bk_permission/update_user/' $old_sys_user->login_role
                        
'/' $this->input->post('login_role') . '/' $id));

                } else {

                    
$this->index();

                }
            }

        }
    }
}