/lib/User.class.php

https://github.com/gwu-libraries/srrs-mobile · PHP · 385 lines · 213 code · 48 blank · 124 comment · 27 complexity · 8f7d9f93cc8a0396676dc8cea2491de1 MD5 · raw file

  1. <?php
  2. /**
  3. * This file contains the User class for viewing
  4. * and manipulating user data
  5. * @author Nick Korbel <lqqkout13@users.sourceforge.net>
  6. * @version 01-28-07
  7. * @package phpScheduleIt
  8. *
  9. * Copyright (C) 2003 - 2007 phpScheduleIt
  10. * License: GPL, see LICENSE
  11. */
  12. $basedir = dirname(__FILE__) . '/..';
  13. include_once($basedir . '/lib/db/UserDB.class.php');
  14. class User {
  15. var $userid; // Properties
  16. var $email; //
  17. var $fname; //
  18. var $lname; //
  19. var $phone; //
  20. var $inst; //
  21. var $position; //
  22. var $perms = null; //
  23. var $emails; //
  24. var $logon_name; //
  25. var $is_admin; //
  26. var $groups = null; //
  27. var $lang;
  28. var $timezone;
  29. var $last_login;
  30. var $is_locked;
  31. var $is_valid = false;
  32. var $err_msg = null;
  33. var $db;
  34. /**
  35. * Sets the userid variable
  36. * @param string $userid users id
  37. */
  38. function User($userid = null) {
  39. $this->userid = $userid;
  40. $this->db = new UserDB();
  41. if (!empty($this->userid)) { // Load values
  42. $this->load_by_id();
  43. }
  44. }
  45. /**
  46. * Returns all data associated with this user's profile
  47. * using their ID as the identifier
  48. * @param none
  49. * @return array of user data
  50. */
  51. function load_by_id() {
  52. $data = $this->db->get_user_data($this->userid);
  53. if (!$data) {
  54. $this->err_msg = $this->db->get_err();
  55. return;
  56. }
  57. else {
  58. $this->is_valid = true;
  59. }
  60. $this->fname = $data['fname'];
  61. $this->lname = $data['lname'];
  62. $this->email = $data['email'];
  63. $this->phone = $data['phone'];
  64. $this->inst = $data['institution'];
  65. $this->position = $data['position'];
  66. $this->logon_name = (isset($data['logon_name']) ? $data['logon_name'] : null);
  67. $this->is_admin = (isset($data['is_admin']) && $data['is_admin'] == 1);
  68. $this->lang = $data['lang'];
  69. $this->timezone = $data['timezone'];
  70. $this->last_login = $data['last_login'];
  71. $this->is_locked = (isset($data['is_locked']) && $data['is_locked'] == 1);
  72. $this->perms = $this->_get_perms();
  73. $this->emails = $this->_get_emails();
  74. $this->groups = $this->_get_groups();
  75. unset($data);
  76. }
  77. /**
  78. * Gets a userid by email
  79. * @param string $email the email address of the User or AnonymousUser
  80. * @return the memberid, if it exists
  81. */
  82. function get_id_by_email($email) {
  83. if ($this->db == null) {
  84. $this->db = new UserDB();
  85. }
  86. return $this->db->get_id_by_email($email);
  87. }
  88. /**
  89. * Returns all permissions for this user
  90. * @param none
  91. * @return array of user permissions with the resource id as the key and 1 as the value
  92. */
  93. function _get_perms() {
  94. global $conf;
  95. return ($conf['app']['use_perms'] ? $this->db->get_user_perms($this->userid) : array());
  96. }
  97. /**
  98. * Checks if the user has permission to use a resource
  99. * @param string $machid id of resource to check
  100. * @return boolean whether user has permission or not
  101. */
  102. function has_perm($machid) {
  103. global $conf;
  104. return ($conf['app']['use_perms'] ? isset($this->perms[$machid]) : true);
  105. }
  106. /**
  107. * Gets the email contact setup for this user
  108. * @param none
  109. * @return array of email settings
  110. */
  111. function _get_emails() {
  112. if (!$emails = $this->db->get_emails($this->userid))
  113. $this->err_msg = $this->db->get_err();
  114. return $emails;
  115. }
  116. /**
  117. * Returns all groups this user belongs to and if they are an admin
  118. * @param none
  119. * @return array groups this user belongs to and if they are an admin. key => groupid, value => array (groupid, group_name, is_admin)
  120. */
  121. function _get_groups() {
  122. return $this->db->get_user_groups($this->userid);
  123. }
  124. /**
  125. * Gets a list of groups that this user is a member of
  126. * @param none
  127. * @return array groups this user belongs to and if they are an admin. key => groupid, value => array (groupid, group_name, is_admin)
  128. */
  129. function get_groups() {
  130. if ($this->groups == null) {
  131. $this->groups = $this->_get_groups();
  132. }
  133. return $this->groups;
  134. }
  135. /**
  136. * Gets a list of group ids that this user is a member of
  137. * @param none
  138. * @return array of group ids this user is a member of
  139. */
  140. function get_groupids() {
  141. if ($this->groups == null) {
  142. $this->groups = $this->_get_groups();
  143. }
  144. return array_keys($this->groups);
  145. }
  146. /**
  147. * Gets a list of all groups that this user is an administrator of
  148. * @param none
  149. * @return array of all groupids that this user is an administrator for
  150. */
  151. function get_admin_groups() {
  152. $admins = array();
  153. $groups = $this->get_groups();
  154. if ($groups != null) {
  155. foreach ($groups as $groupid => $data) {
  156. if ((int)$data['is_admin'] == 1) {
  157. $admins[] = $groupid;
  158. }
  159. }
  160. }
  161. return $admins;
  162. }
  163. /**
  164. * Returns all permissions for this user
  165. * @param none
  166. * @return array of permissions with key => machid value => resource name
  167. */
  168. function get_perms() {
  169. if ($this->perms == null) {
  170. $this->perms = $this->_get_perms();
  171. }
  172. return $this->perms;
  173. }
  174. /**
  175. * Returns whether the user wants the type of email contact or not
  176. * @param string $type email contact type.
  177. * Valid types are 'e_add', 'e_mod', 'e_del' for adding/modifying/deleting reservations, respectively
  178. * @return boolean whether user wants the email or not
  179. */
  180. function wants_email($type) {
  181. return ($this->emails[$type] == 'y');
  182. }
  183. /**
  184. * Whether the user wants html or plain text emails
  185. * @param none
  186. * @return whether they want html email or not
  187. */
  188. function wants_html() {
  189. return ($this->emails['e_html'] == 'y');
  190. }
  191. /**
  192. * Sets the users email preferences
  193. * @param string $e_add value to set e_add field to
  194. * @param string $e_mod value to set e_mod field to
  195. * @param string $e_del value to set e_del field to
  196. * @param string $e_app value to set e_app field to
  197. * @param string $e_html value to set e_html field to
  198. */
  199. function set_emails($e_add, $e_mod, $e_del, $e_app, $e_html) {
  200. $this->db->set_emails($e_add, $e_mod, $e_del, $e_app, $e_html, $this->userid);
  201. }
  202. /**
  203. * Return all user data in an array
  204. * @param none
  205. * @return assoc array of all user data
  206. */
  207. function get_user_data() {
  208. return array (
  209. 'memberid' => $this->userid,
  210. 'email' => $this->email,
  211. 'fname' => $this->fname,
  212. 'lname' => $this->lname,
  213. 'phone' => $this->phone,
  214. 'institution'=> $this->inst,
  215. 'position' => $this->position,
  216. 'perms' => $this->perms,
  217. 'logon_name'=> $this->logon_name,
  218. 'groups' => $this->groups,
  219. 'lang' => $this->lang,
  220. 'timezone' => $this->timezone
  221. );
  222. }
  223. /**
  224. * Sets a users password
  225. * @param string $new_password the new password to set for this user
  226. */
  227. function set_password($new_password) {
  228. $this->db->set_password($new_password, $this->userid);
  229. }
  230. /**
  231. * Adds the user to the list of groups
  232. * @param array $groups array of groups to add the user to
  233. */
  234. function add_groups($groups) {
  235. $this->db->add_groups($this->userid, $groups);
  236. $this->groups = null; // Will reload on the next call to get_groups()
  237. }
  238. /**
  239. * Removes the user from the list of groups
  240. * @param array $groups array of groups to remove the user from
  241. */
  242. function remove_groups($groups) {
  243. $this->db->remove_groups($this->userid, $groups);
  244. $this->groups = null; // Will reload on the next call to get_groups()
  245. }
  246. /**
  247. * Returns whether this user is valid or not
  248. * @param none
  249. * @return boolean if user is valid or not
  250. */
  251. function is_valid() {
  252. return $this->is_valid;
  253. }
  254. /**
  255. * Returns whether this user is an admin of the group or not
  256. * @param array $groupids (optional) the group ids to check for admin on. if this is not provided, this just checks if the user is an admin of any group
  257. * @return if the user is the group admin or not
  258. */
  259. function is_group_admin($groupids = null) {
  260. $admin_groups = $this->get_admin_groups();
  261. if ( !is_null($groupids) ) {
  262. if ( count($admin_groups) <= 0 ) {
  263. return false; // No groups, so can't be an admin
  264. }
  265. for ($i = 0; $i < count($groupids); $i++) {
  266. if ( array_search($groupids[$i], $admin_groups) !== false ) {
  267. return true; // Admin of at least one of the groups
  268. }
  269. }
  270. }
  271. else {
  272. return count($admin_groups) > 0;
  273. }
  274. }
  275. /**
  276. * Stores the user's language preference to the database
  277. * @param string $lang the language key
  278. */
  279. function set_lang($lang) {
  280. $this->db->set_lang($this->userid, $lang);
  281. }
  282. /**
  283. * Updates user's last_login date in the datapase
  284. * @param string $date contains date to be set in DB format
  285. */
  286. function set_last_login($date){
  287. $this->db->set_last_login($this->userid, $date);
  288. }
  289. /**
  290. * Returns the error message generated
  291. * @param none
  292. * @return error message as string
  293. */
  294. function get_error() {
  295. return $this->err_msg;
  296. }
  297. function get_id() {
  298. return $this->userid;
  299. }
  300. function get_fname() {
  301. return $this->fname;
  302. }
  303. function get_lname() {
  304. return $this->lname;
  305. }
  306. function get_name() {
  307. return $this->fname . ' ' . $this->lname;
  308. }
  309. function get_email() {
  310. return $this->email;
  311. }
  312. function get_phone() {
  313. return $this->phone;
  314. }
  315. function get_inst() {
  316. return $this->inst;
  317. }
  318. function get_position() {
  319. return $this->position;
  320. }
  321. function get_isadmin() {
  322. return $this->is_admin;
  323. }
  324. function get_logon_name() {
  325. return $this->logon_name;
  326. }
  327. function get_lang() {
  328. return $this->lang;
  329. }
  330. function get_timezone() {
  331. return $this->timezone;
  332. }
  333. function get_islocked(){
  334. return $this->is_locked;
  335. }
  336. }
  337. ?>