1
|
|
|
<?php defined('BASEPATH') OR exit('No direct script access allowed'); |
2
|
|
|
|
3
|
|
|
class Temporary_account_model extends CI_Model { |
4
|
|
|
|
5
|
|
|
public function __construct() { |
6
|
|
|
parent::__construct(); |
7
|
|
|
$this->load->helper('date'); |
8
|
|
|
} |
9
|
|
|
|
10
|
|
|
public function create($logged, $faculty, $department, $requester, $first_name, $last_name, $email, $username, $expiry) { |
11
|
|
|
|
12
|
|
|
$firstchar = substr($this->input->post('first_name'), 0, 1); |
13
|
|
|
$builtUsername = strtolower($firstchar) . strtolower($this->input->post('last_name')); |
14
|
|
|
|
15
|
|
|
//check user name is new |
16
|
|
|
$username = $this->config->item('ldapshortdomain') . $builtUsername; |
17
|
|
|
// specify the LDAP server to connect to |
18
|
|
|
$conn = ldap_connect($this->config->item('ldapserver')) or die("Oh no can't create LDAP connection"); |
19
|
|
|
// bind to the LDAP server specified above |
20
|
|
View Code Duplication |
if (!ldap_bind($conn, $this->config->item('ldapbindun'),"@".$this->config->item('ldapdomain'), $this->config->item('ldapbindpass'))) |
|
|
|
|
21
|
|
|
echo "Invalid credentials."; |
22
|
|
|
// Search for user in directory |
23
|
|
|
$cred = explode('\\', $username); |
24
|
|
|
list($domain, $user) = $cred; |
|
|
|
|
25
|
|
|
|
26
|
|
|
$result = ldap_search($conn, $this->config->item('ldapuserou'), "samaccountname=$user"); |
27
|
|
|
// get entry data as array |
28
|
|
|
$info = ldap_count_entries($conn, $result); |
29
|
|
|
|
30
|
|
|
if ($info != 0) { |
31
|
|
|
$builtUsername = $builtUsername . "2"; |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
function randomPassword() { |
|
|
|
|
35
|
|
|
$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; |
36
|
|
|
$pass = array(); //remember to declare $pass as an array |
37
|
|
|
$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache |
38
|
|
|
for ($i = 0; $i < 6; $i++) { |
39
|
|
|
$n = rand(0, $alphaLength); |
40
|
|
|
$pass[] = $alphabet[$n]; |
41
|
|
|
} |
42
|
|
|
return implode($pass); //turn the array into a string |
43
|
|
|
} |
44
|
|
|
$generatedpassword = randomPassword(); |
45
|
|
|
|
46
|
|
|
/** Returns UNIX timestamp and Active Directory timestamp for given date and time */ |
47
|
|
|
//Format dd-mm-yyyy |
48
|
|
|
$dateFromForm = $this->input->post('expiry'); |
49
|
|
|
|
50
|
|
|
//Format hh:mm:ss |
51
|
|
|
$timeFromForm = "00:00:00"; |
52
|
|
|
$dateWithTime = $dateFromForm . " " . $timeFromForm; |
|
|
|
|
53
|
|
|
|
54
|
|
View Code Duplication |
function convertDateToUnix($input) { |
|
|
|
|
55
|
|
|
$format = 'd-m-Y H:i:s'; |
56
|
|
|
$date = DateTime::createFromFormat($format, $input); |
57
|
|
|
$UNIXTimestamp = $date->getTimestamp(); |
58
|
|
|
return $UNIXTimestamp; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
function convertUnixtoWin($input) { |
|
|
|
|
62
|
|
|
return ($input + 11644473600) * 10000000; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
//$UNIX = convertDateToUnix($dateWithTime); |
|
|
|
|
66
|
|
|
//$WIN = convertUnixtoWin($UNIX); |
|
|
|
|
67
|
|
|
|
68
|
|
|
$data = array( |
69
|
|
|
'logged' => $_SESSION['ldap']['full_name'], |
70
|
|
|
'logged_at' => date("Y-m-d H:i:s", time()), |
71
|
|
|
'requester' => $this->input->post('requester'), |
72
|
|
|
'faculty' => $this->input->post('faculty'), |
73
|
|
|
'department' => $this->input->post('department'), |
74
|
|
|
'first_name' => $this->input->post('first_name'), |
75
|
|
|
'last_name' => $this->input->post('last_name'), |
76
|
|
|
'email' => $this->input->post('email'), |
77
|
|
|
'username' => $this->input->post('username'), |
78
|
|
|
'expiry' => $this->input->post('expiry'), |
79
|
|
|
'password' => $generatedpassword, |
80
|
|
|
//'wintime' => $WIN, |
|
|
|
|
81
|
|
|
//'unixtime' => $UNIX, |
|
|
|
|
82
|
|
|
'status' => 0); |
83
|
|
|
|
84
|
|
|
return $this->db->insert('temporary_account', $data); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function get_next_temp() { |
88
|
|
|
$query = $this->db->query("SELECT id FROM temporary_account ORDER BY id DESC LIMIT 1;"); |
89
|
|
|
foreach ($query->result() as $row) { |
90
|
|
|
return $row->id + 1; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
View Code Duplication |
function get_faculty() { |
|
|
|
|
95
|
|
|
$this->db->from('faculty'); |
96
|
|
|
$this->db->order_by('faculty'); |
97
|
|
|
$result = $this->db->get(); |
98
|
|
|
$return = array(); |
99
|
|
|
if ($result->num_rows() > 0) { |
100
|
|
|
foreach ($result->result_array() as $row) { |
101
|
|
|
$return[$row['cat_id']] = $row['faculty']; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $return; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
View Code Duplication |
function get_department() { |
|
|
|
|
109
|
|
|
if (!isset($_GET['cat'])) { |
110
|
|
|
$_GET['cat'] = '1'; |
111
|
|
|
} |
112
|
|
|
$result = $this->db->get_where('facultysub', array('cat_id' => $_GET['cat'])); |
113
|
|
|
$return = array(); |
114
|
|
|
if ($result->num_rows() > 0) { |
115
|
|
|
foreach ($result->result_array() as $row) { |
116
|
|
|
$return[$row['cat_key']] = $row['subfaculty']; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $return; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function get_pending() { |
124
|
|
|
$this->db->where('status', '0'); |
125
|
|
|
$query = $this->db->get('temporary_account'); |
126
|
|
|
return $query->result_array(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function get_all() { |
130
|
|
|
$query = $this->db->get('temporary_account'); |
131
|
|
|
return $query->result_array(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function check_status() { |
135
|
|
|
$this->db->where('logged', $_SESSION['ldap']['full_name'])->or_where('requester', $_SESSION['ldap']['full_name']); |
136
|
|
|
$query = $this->db->get('temporary_account'); |
137
|
|
|
return $query->result_array(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function match_id_user($id) { |
141
|
|
|
$this->db->where('id', $id); |
142
|
|
|
$query = $this->db->get('temporary_account'); |
143
|
|
|
return $query->result_array(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function cancel($id) { |
147
|
|
|
|
148
|
|
|
$this->db->where('id', $id); |
149
|
|
|
$this->db->from('temporary_account'); |
150
|
|
|
$data = array( |
151
|
|
|
'status' => '3', |
152
|
|
|
); |
153
|
|
|
return $this->db->update('temporary_account', $data); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
View Code Duplication |
public function reject($id) { |
|
|
|
|
157
|
|
|
|
158
|
|
|
$this->db->where('id', $id); |
159
|
|
|
$this->db->from('temporary_account'); |
160
|
|
|
$data = array( |
161
|
|
|
'status' => '2', |
162
|
|
|
'status_at' => date("Y-m-d H:i:s", time()), |
163
|
|
|
'status_by' => $_SESSION['ldap']['full_name'], |
164
|
|
|
); |
165
|
|
|
return $this->db->update('temporary_account', $data); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
View Code Duplication |
public function approve($id) { |
|
|
|
|
169
|
|
|
|
170
|
|
|
$this->db->where('id', $id); |
171
|
|
|
$this->db->from('temporary_account'); |
172
|
|
|
$data = array( |
173
|
|
|
'status' => '1', |
174
|
|
|
'status_at' => date("Y-m-d H:i:s", time()), |
175
|
|
|
'status_by' => $_SESSION['ldap']['full_name'], |
176
|
|
|
); |
177
|
|
|
|
178
|
|
|
return $this->db->update('temporary_account', $data); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function created_account($id) { |
182
|
|
|
|
183
|
|
|
$this->db->where('id', $id); |
184
|
|
|
$this->db->from('temporary_account'); |
185
|
|
|
$data = array( |
186
|
|
|
'created' => '1', |
187
|
|
|
); |
188
|
|
|
return $this->db->update('temporary_account', $data); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function get_account($id) { |
192
|
|
|
$this->db->where('id', $id); |
193
|
|
|
$query = $this->db->get('temporary_account'); |
194
|
|
|
return $query->result_array(); |
195
|
|
|
} |
196
|
|
|
} |
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.