My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
254
255
256
257
258
259
260
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* ZonesCI library
*
* Allows integrating user authentication to existing project.
*
* ZonesCI allows you to:
*
* -add user roles to existing projects
* -skip database alterations
* -add different roles
* -check pages for logged groups of users
*
* Groups are defined as permissions - each page
* can be viewed by list of groups. Every user could
* participate in more than one groups. This does
* adding and removing rights to users easier and
* many-to-many relation doesn't require group_id alteration
* in 'users' table
*
*
*/
class Zonesci
{
var $CI;
var $users;
var $groups;
var $user_groups;

/**
* username and password columns as defined in DB
*/
var $user_id;
var $user_login;
var $user_password;

/**
* Library constructor -
* load CI object and database meta data
*
*/
function Zonesci()
{
$this->CI =& get_instance();
$this->CI->load->config('zonesci.php');

$this->users = $this->CI->config->item('z_users');
$this->groups = $this->CI->config->item('z_groups');
$this->user_groups = $this->CI->config->item('z_user_groups');
$this->user_login = $this->CI->config->item('z_user_login');
$this->user_password = $this->CI->config->item('z_user_password');
$this->user_id = $this->CI->config->item('z_user_id');
}

/**
* Create a user account
*
* @access public
* @param string
* @param string
* @param bool
* @return bool
*/
function create($user = '', $password = '', $auto_login = true) {
//Make sure account info was sent
if($user == '' OR $password == '') {
return false;
}

//Check against user table
$this->CI->db->where($this->user_login, $user);
$query = $this->CI->db->getwhere($this->users);

if ($query->num_rows() > 0) {
//username already exists
return false;

} else {
//Encrypt password
$password = md5($password);

//Insert account into the database
$data = array(
$this->user_login => $user,
$this->user_password => $password
);
$this->CI->db->set($data);
if(!$this->CI->db->insert($this->users)) {
//There was a problem!
return false;
}
$user_id = $this->CI->db->insert_id();

//Automatically login to created account
if($auto_login) {
//Destroy old session
$this->CI->session->sess_destroy();

//Create a fresh, brand new session
$this->CI->session->sess_create();

//Set session data
$this->CI->session->set_userdata(array($this->user_id => $user_id, $this->user_login => $user));

//Set logged_in to true
$this->CI->session->set_userdata(array('logged_in' => true));

}

//Login was successful
return true;
}

}

/**
* Delete user
*
* @access public
* @param integer
* @return bool
*/
function delete($user_id) {
if(!is_numeric($user_id)) {
//There was a problem
return false;
}

if($this->CI->db->delete($this->users, array($this->user_id => $user_id))) {
//Database call was successful, user is deleted
return true;
} else {
//There was a problem
return false;
}
}


/**
* Login and sets session variables
*
* @access public
* @param string
* @param string
* @return bool
*/
function login($user = '', $password = '') {
//Make sure login info was sent
if($user == '' OR $password == '') {
return false;
}

/*
* Check against user table
* join Many-to-Many groups table and Groups table, too
* and check permissions (add to session)
*/

// DEBUG ON

// TODO: improper select statement!!!! fix joins
$this->CI->db->_compile_select();

$this->CI->db->where($this->user_login, $user);
$this->CI->db->join($this->user_groups, $this->users. '.'. $this->user_id. '='. $this->user_groups. '.'.$this->user_id , 'left');
$this->CI->db->join($this->groups, $this->groups. '.group_id = '. $this->user_groups. '.group_id', 'left');

$query = $this->CI->db->getwhere($this->users);

// DEBUG OFF
$sql_joins = $this->CI->db->last_query();

if ($query->num_rows() > 0) {
$row = $query->row_array();

//Check against password
if(md5($password) != $row[$this->user_password]) {
return false;
}

//Destroy old session
$this->CI->session->sess_destroy();

//Create a fresh, brand new session
$this->CI->session->sess_create();

// extract groups in array
$groups = array();
foreach($query->result_array() as $res) {
$groups[] = $res['group_name'];
}

//Set session data
$this->CI->session->set_userdata(array('id' => $row[$this->user_id]));
$this->CI->session->set_userdata(array('username' => $row[$this->user_login]));
$this->CI->session->set_userdata(array('group' => $groups));

// DEBUG ON
$this->CI->session->set_userdata(array('sql' => $sql_joins));
$this->CI->session->set_userdata(array('resarray' => $query->result_array()));

// DEBUG OFF
$this->CI->session->set_userdata(array('logged_in' => true));

//Login was successful
return true;
} else {
//No database result found
return false;
}

}

/**
* Logout user
*
* @access public
* @return void
*/
function logout() {
//Destroy session
$this->CI->session->sess_destroy();
}

function check($groups = '')
{
if( ! $this->CI->session->userdata('logged_in'))
{
show_404('page');
return;
}
else
{
return $this->does_allow($groups);
}
}

private function does_allow($groups = '') {
if($groups == '') {
return false;
}
$sess_groups = $this->CI->session->userdata('group');
if(is_array($groups)) {
foreach($groups as $group) {
if(in_array($group, $sess_groups)) {
return true;
}
}
}
else {
if(in_array($groups, $sess_groups)) {
return true;
}
}
return false;
}
}

/* End of file Zonesci.php */
/* Location: ./system/application/libraries/Zonesci.php */

Change log

r2 by nofearinc on Dec 12, 2009   Diff
[No log message]
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 6246 bytes, 260 lines
Powered by Google Project Hosting