Temporary_account::check()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
defined('BASEPATH') OR exit('No direct script access allowed');
4
5
class Temporary_account extends My_Force_Login {
6
7
    public function __construct() {
8
        parent::__construct();
9
        $this->load->model('computing-support/Temporary_account_model', 'temporary_account_model');
10
    }
11
12
    public function index() {
13
14
        $this->load->helper('form');
15
        $this->load->library('form_validation');
16
17
        $data = array();
18
        $data['faculty'] = $this->temporary_account_model->get_faculty();
19
        $data['department'] = $this->temporary_account_model->get_department();
20
        $data['tempid'] = "tempuser" . $this->temporary_account_model->get_next_temp();
21
22
        //validation
23
        $this->form_validation->set_rules('faculty', 'Faculty', 'trim|required');
24
        $this->form_validation->set_rules('requester', 'Staff requester', 'trim|required|min_length[3]');
25
        $this->form_validation->set_rules('first_name', 'Users first name', 'trim|required|min_length[3]');
26
        $this->form_validation->set_rules('last_name', 'Users last name', 'trim|required|min_length[3]');
27
28
        if ($this->form_validation->run() === FALSE) {
29
30
            $this->load->view('templates/header');
31
            $this->load->view('computing-support/temporary-account/view', $data);
32
            $this->load->view('templates/footer');
33
        } else {
34
35
            $logged = $this->input->post('logged');
36
            $faculty = $this->input->post('faculty');
37
            $department = $this->input->post('department');
38
            $requester = $this->input->post('requester');
39
            $first_name = $this->input->post('first_name');
40
            $last_name = $this->input->post('last_name');
41
            $email = $this->input->post('email');
42
            $username = $this->input->post('username');
43
            $expiry = $this->input->post('expiry');
44
45
            if ($this->temporary_account_model->create($logged, $faculty, $department, $requester, $first_name, $last_name, $email, $username, $expiry)) {
46
47
                $this->email->from('[email protected]', 'Temporary Logon Account Request');
48
                $this->email->to('[email protected]');
49
                $this->email->subject('Temporary Logon Account Request');
50
                $this->email->message('A temporary network account has been requested by ' . $_SESSION['ldap']['full_name']
51
                        . ' for ' . $this->input->post('requester')
52
                        . ''
53
                        . 'the user will be: ' . $this->input->post('first_name') . ' ' . $this->input->post('last_name')
54
                        . ''
55
                        . 'Approve https://intranet.cant-col.ac.uk/dashboard/computing-support/temporary-account/approve');
56
                $this->email->send();
57
58
                $this->load->view('templates/header');
59
                $this->load->view('computing-support/temporary-account/complete');
60
                $this->load->view('templates/footer');
61
            } else {
62
63
                $data = array();
0 ignored issues
show
Unused Code introduced by
$data is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
64
                $data = new stdClass();
65
                $data->error = 'There was a problem requesting this account. Please try again.';
66
67
                // failed to create user
68
                $this->load->view('templates/header');
69
                $this->load->view('computing-support/temporary-account/view', $data);
70
                $this->load->view('templates/footer');
71
            }
72
        }
73
    }
74
    
75 View Code Duplication
    public function history() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
77
        if (in_array('CN=DG06,OU=Distribution Groups,OU=Email Groups,OU=Accounts,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups'])) {
78
79
            $data = array();
80
            $data['temporary_account'] = $this->temporary_account_model->get_all();
81
82
            $this->load->view('templates/header');
83
            $this->load->view('computing-support/temporary-account/history', $data);
84
            $this->load->view('templates/footer');
85
        } else {
86
            redirect('permissions');
87
        }
88
    }
89
    
90
    public function check() {
91
92
        $data = array();
93
        $data['temporary_account'] = $this->temporary_account_model->check_status();
94
95
        $this->load->view('templates/header');
96
        $this->load->view('computing-support/temporary-account/check', $data);
97
        $this->load->view('templates/footer');
98
    }
99
100 View Code Duplication
    public function pending() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
102
        if (in_array('CN=DG06,OU=Distribution Groups,OU=Email Groups,OU=Accounts,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups'])) {
103
104
            $data = array();
105
            $data['temporary_account'] = $this->temporary_account_model->get_pending();
106
107
            $this->load->view('templates/header');
108
            $this->load->view('computing-support/temporary-account/pending', $data);
109
            $this->load->view('templates/footer');
110
        } else {
111
            redirect('permissions');
112
        }
113
    }
114
    
115
    public function cancel() {
116
        
117
        $id = $_GET['id'];
118
        $check_user = $this->temporary_account_model->match_id_user($id);
119
        if ($check_user[0]['logged'] == $_SESSION['ldap']['full_name'] || $check_user[0]['requested'] == $_SESSION['ldap']['full_name']) {
120
121
            if (isset($_GET['id'])) {
122
123
                $id = $_GET['id'];
124
                $this->temporary_account_model->cancel($id);
125
126
                $function = 'temp_account_CANCEL_' . $id;
127
                $this->user_model->function_log($function);
128
129
                redirect($_SERVER['HTTP_REFERER']);
130
            }
131
            redirect($_SERVER['HTTP_REFERER']);
132
        } else {
133
            redirect($_SERVER['HTTP_REFERER']);
134
        }
135
    }
136
    
137 View Code Duplication
    public function reject() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
139
        if (in_array('CN=DG06,OU=Distribution Groups,OU=Email Groups,OU=Accounts,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups'])) {
140
141
            if (isset($_GET['id'])) {
142
143
                $id = $_GET['id'];
144
                $this->temporary_account_model->reject($id);
145
146
                $function = 'temp_account_REJECT_' . $id;
147
                $this->user_model->function_log($function);
148
149
                redirect($_SERVER['HTTP_REFERER']);
150
            }
151
152
            redirect($_SERVER['HTTP_REFERER']);
153
        } else {
154
            redirect($_SERVER['HTTP_REFERER']);
155
        }
156
    }
157
158
    public function approve() {
159
160
        if (in_array('CN=DG06,OU=Distribution Groups,OU=Email Groups,OU=Accounts,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups'])) {
161
162
            if (isset($_GET['id'])) {
163
                
164
                $id = $_GET['id'];
165
                //$this->temporary_account_model->approve($id);
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
166
167
                $function = 'temp_account_APPROVED_' . $id;
168
                $this->user_model->function_log($function);
169
        
170
                    $data = array();
171
                    $data['AD'] = $this->temporary_account_model->get_account($id);
172
173
                    //AD account start
174
                    //AD Server
175
                    $AD_server = $this->config->item('ldapserver');
176
                    $AD_Auth_User = $this->config->item('ldapshortdomain').$this->config->item('ldapadminun'); //Administrative user
177
                    $AD_Auth_PWD = $this->config->item('ldapadminpass'); //The password
178
                    //Create Active Directory timestamp
179
                    date_default_timezone_set($this->config->item('timezone'));
180
181
                    //Format dd-mm-yyyy
182
                    //Expiry is beginning of day (thats why +1 day
183
                    $dateFromForm = date('d-m-Y', strtotime($data['AD'][0]['expiry']. ' +1 day'));
184
185
                    //Format hh:mm:ss
186
                    $timeFromForm = "00:00:00";
187
188
                    $dateWithTime = $dateFromForm . " " . $timeFromForm;
0 ignored issues
show
Unused Code introduced by
$dateWithTime is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
189
190 View Code Duplication
                    function convertDateToUnix($input) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
191
                        $format = 'd-m-Y H:i:s';
192
                        $date = DateTime::createFromFormat($format, $input);
193
                        $UNIXTimestamp = $date->getTimestamp();
194
                        return $UNIXTimestamp;
195
                    }
196
197
                    function convertUnixtoWin($input) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
198
                        return ($input + 11644473600) * 10000000;
199
                    }
200
201
                    //$UNIX = convertDateToUnix($dateWithTime);
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
202
                    //$WIN = convertUnixtoWin($UNIX);
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
203
204
                    //Create Unicode password
205
                    $passwordWithQuotes = '"' . $data['AD'][0]['password'] . '"';
206
                    $ldaprecord = array();
207
                    $ldaprecord["unicodepwd"] = iconv('UTF-8', 'UTF-16LE', $passwordWithQuotes);
208
209
                    //Build Active Directory record     
210
                    $ldaprecord["cn"] = $data['AD'][0]['username'];
211
                    $ldaprecord["givenName"] = $data['AD'][0]['first_name'];
212
                    $ldaprecord["sn"] = $data['AD'][0]['last_name'];
213
                    $ldaprecord["mail"] = $data['AD'][0]['email'];
214
                    $ldaprecord["sAMAccountName"] = $data['AD'][0]['username'];
215
                    $ldaprecord["displayName"] = $data['AD'][0]['first_name'] . " " . $data['AD'][0]['last_name'];
216
                    $ldaprecord["l"] = "Canterbury";
217
                    $ldaprecord["description"] = "Temp account created by dashboard for " . $ldaprecord["displayName"];
218
                    //$ldaprecord["accountExpires"] = $WIN;
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
219
                    $ldaprecord["UserAccountControl"] = "512"; //512 - Enabled Account
220
                    $ldaprecord['userPrincipalName'] = $data['AD'][0]['username'] . '@cant-col.ac.uk';
221
                    $ldaprecord['objectclass'][0] = "top";
222
                    $ldaprecord['objectclass'][1] = "person";
223
                    $ldaprecord['objectclass'][2] = "organizationalPerson";
224
                    $ldaprecord['objectclass'][3] = "user";
225
                    $dn = 'CN=' . $ldaprecord["cn"] . ',OU=Temp Accounts,OU=Students,OU=Accounts,DC=cant-col,DC=ac,DC=uk';
226
227
                    // Connect to Active Directory
228
                    $ds = ldap_connect('ldaps://' . $AD_server);
229
230
                    if ($ds) {
231
                        ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
232
                        ldap_bind($ds, $AD_Auth_User, $AD_Auth_PWD); //Bind
233
                        ldap_add($ds, $dn, $ldaprecord); //Create account
234
                        ldap_close($ds); //Close connection
235
                        $this->temporary_account_model->created_account($id);
236
                    } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
237
                        //redirect('computing-support/temporary-account/error?id='.$id);
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
238
                    }
239
                    //AD account end.
240
            
241
                //redirect($_SERVER['HTTP_REFERER']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
88% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
242
                    $this->load->view('templates/header');
243
            $this->load->view('computing-support/temporary-account/view');
244
            $this->load->view('templates/footer');
245
                    
246
            }
247
248
            //redirect($_SERVER['HTTP_REFERER']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
88% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
249
        } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
250
            //redirect($_SERVER['HTTP_REFERER']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
88% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
251
        }
252
    }
253
    
254
    public function error() {
255
256
        if (in_array('CN=DG06,OU=Distribution Groups,OU=Email Groups,OU=Accounts,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups'])) {
257
258
            $this->load->view('templates/header');
259
            $this->load->view('computing-support/temporary-account/error', $data);
0 ignored issues
show
Bug introduced by
The variable $data does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
260
            $this->load->view('templates/footer');
261
        } else {
262
            redirect('permissions');
263
        }
264
    }
265
}
266