-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.class.php
145 lines (114 loc) · 4.19 KB
/
User.class.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
<?php
class User {
//
// Given a GUID string, fetches that user's data from the local SQL database.
// We use the GUID in case a user with username 'foo' comes and goes, then someone ELSE
// with the same name (and resultant 'foo' username') has a new account set up.
private function loadLocalDataByGuid($guid) {
// Query database for user
$info_sth = $this->core->prepareQuery(
"SELECT system_user_id,
username,
firstName,
lastName,
email,
active,
ldap_objectguid,
case_man
FROM system_users
WHERE ldap_objectguid=?
AND active=1;"
);
$info_result = $info_sth->execute( array($guid) );
# If user does not exist in ils system_users table and search query was able to prepare, create that new user here
if( ( $info_sth->rowCount() != 1) && ( $info_result ) ){
// Prepare query for entry into database
$new_user_info_sth = $this->core->prepareQuery(
"INSERT INTO `system_users`
(
system_role_id,
username,
firstName,
lastName,
email,
ldap_objectguid,
current
) VALUES (
?, # 'system_role_id' - Set to default of 1 Default Role
?, # 'username'
?, # 'firstName'
?, # 'lastName'
?, # 'email'
?, # 'ldap_objectguid'
? # 'current' - Set to default of 1. No db default set.
)"
);
// Execute query to add new user
$new_user_info_sth_result = $new_user_info_sth->execute( array(
1,
$this->ldap_data['samaccountname'][0],
$this->ldap_data['givenname'][0],
$this->ldap_data['sn'][0],
$this->ldap_data['mail'][0],
$guid,
1
) );
// Throw error if insert fails
if( !$new_user_info_sth_result ) throw new Exception( 'Query to add new user to ILS system failed.' );
// Prepare query for getting new user's system_user_id
$system_user_id_get_prep = $this->core->prepareQuery(
"SELECT
system_user_id
FROM
system_users
WHERE
ldap_objectguid=?
AND active=1;"
);
// If query to add new user is sucessful, execute query to get that new user's system_user_id
if( $new_user_info_sth_result ){
$system_user_id_get_result = $system_user_id_get_prep->execute( array( $guid ) );
};
// Throw error get system_user_id query fails
if( !$system_user_id_get_result ) throw new Exception('New user ID Query Failed');
if( $system_user_id_get_prep->rowCount() != 1 ) throw new Warning('Query to get system user ID for new user failed.');
// Get new system user id and set to variable
$system_user_result_array = $system_user_id_get_prep->fetch(PDO::FETCH_ASSOC);
$new_system_user_id = $system_user_result_array['system_user_id'];
// If $system_user_id_get_result is true, then we add a record to system_user_to_role so that permissions work
if( $system_user_id_get_result ){
// Prepare query for entry into database
$new_user_to_role_sth = $this->core->prepareQuery(
"INSERT INTO `system_user_to_role`
(
system_user_id,
system_role_id
) VALUES (
?, # 'system_user_id' -
? # 'system_role_id' - Set to default of 7 Default Role. No db default set.
)"
);
}
// Execute query to add new system_user_to_role record so permissions work
$new_user_to_role_sth_result = $new_user_to_role_sth->execute( array(
$new_system_user_id, # 'system_user_id'
7 # 'system_role_id'
) );
// Throw error get system_user_id query fails
if( !$new_user_to_role_sth_result ) throw new Exception('Query to add system user to role record failed');
if( $new_user_to_role_sth->rowCount() != 1 ) throw new Warning('Query to add system user role failed.');
// Once data is added, take the results and set equal $info_sth.
$info_sth = $new_user_info_sth;
// By doing this, the code should continue on with the new information as if it was a user that already existed
return $info_sth;
}// End if function to handle adding a new system user
# Fetch data & store
$data = $info_sth->fetch(PDO::FETCH_ASSOC);
if($data === false) throw new Exception('Could not fetch user data');
$this->sql_data = $data;
// Here we do stuff with the local user data we've pulled
return true;
}// End loadLocalDataByGuid
//.....Other code
}
?>