/secure/managers/PermissionManager.php

https://github.com/gogbuehi/BMCD.com · PHP · 227 lines · 188 code · 14 blank · 25 comment · 11 complexity · c8c276963cd0bfd604843b67720f85e8 MD5 · raw file

  1. <?php
  2. require_once 'managers/BaseManager.php';
  3. require_once 'exceptions/PermissionException.php';
  4. require_once 'models/database_object.php';
  5. require_once 'models/session.php';
  6. require_once 'models/user.php';
  7. /**
  8. * Description of PermissionManager
  9. *
  10. * Manages permissions for viewers and users of the site
  11. *
  12. * @author Goodwin
  13. */
  14. class PermissionManager extends BaseManager {
  15. const LOGIN_SERVICE_NAME='LoginService';
  16. const LOGIN_SERVICE_PARAM_USERNAME='username';
  17. const LOGIN_SERVICE_PARAM_PASSWORD='password';
  18. const LOGIN_SERVICE_PARAM_DOMAIN='domain';
  19. const LOGOUT_SERVICE_NAME='LogoutService';
  20. const CHANGE_PASSWORD_SERVICE_NAME='ChangePasswordService';
  21. const CHANGE_PASSWORD_SERVICE_PARAM_PASSWORD='password';
  22. const CHANGE_PASSWORD_SERVICE_PARAM_NEW_PASSWORD='newPassword';
  23. const CHANGE_PASSWORD_SERVICE_PARAM_NEW_PASSWORD_CONFIRM='newPasswordConfirm';
  24. const CHANGE_PASSWORD_SERVICE_PARAM_DOMAIN='domain';
  25. const FORGOT_PASSWORD_SERVICE_NAME='ForgotPasswordService';
  26. const FORGOT_PASSWORD_SERVICE_PARAM_USERNAME='username';
  27. public $services = array(
  28. self::LOGIN_SERVICE_NAME => array(
  29. self::LOGIN_SERVICE_PARAM_USERNAME => self::PARAM_TYPE_STRING,
  30. self::LOGIN_SERVICE_PARAM_PASSWORD => self::PARAM_TYPE_STRING,
  31. self::LOGIN_SERVICE_PARAM_DOMAIN => self::PARAM_TYPE_STRING
  32. ),
  33. self::LOGOUT_SERVICE_NAME => array(),
  34. self::CHANGE_PASSWORD_SERVICE_NAME => array(
  35. self::CHANGE_PASSWORD_SERVICE_PARAM_PASSWORD => self::PARAM_TYPE_STRING,
  36. self::CHANGE_PASSWORD_SERVICE_PARAM_NEW_PASSWORD => self::PARAM_TYPE_STRING,
  37. self::CHANGE_PASSWORD_SERVICE_PARAM_NEW_PASSWORD_CONFIRM => self::PARAM_TYPE_STRING
  38. ),
  39. self::FORGOT_PASSWORD_SERVICE_NAME => array(
  40. self::FORGOT_PASSWORD_SERVICE_PARAM_USERNAME => self::PARAM_TYPE_STRING
  41. )
  42. );
  43. protected $tLog;
  44. protected $session;
  45. function __construct() {
  46. global $tLog;
  47. $this->tLog = &$tLog;
  48. //Get the current session
  49. $this->session = new Session();
  50. }
  51. function handleRequest(ServiceRequest $request,$params) {
  52. $serviceName = $request->getServiceName();
  53. $response = new ServiceResponse($serviceName,$request->getType());
  54. switch($serviceName) {
  55. case self::LOGIN_SERVICE_NAME:
  56. return $this->loginRequest($response,$params);
  57. break;
  58. case self::CHANGE_PASSWORD_SERVICE_NAME:
  59. return $this->changePasswordRequest($response, $params);
  60. break;
  61. case self::LOGOUT_SERVICE_NAME:
  62. return $this->logoutRequest($response);
  63. break;
  64. case self::FORGOT_PASSWORD_SERVICE_NAME:
  65. return $this->forgotPasswordRequest($response, $params);
  66. break;
  67. default:
  68. $msg = 'Unkown Service: '.$serviceName;
  69. throw new ServiceException($msg,ServiceException::CODE_INVALID_SERVICE);
  70. }
  71. }
  72. function loginRequest(ServiceResponse &$response,$params) {
  73. try {
  74. $this->login($params[self::LOGIN_SERVICE_PARAM_USERNAME], $params[self::LOGIN_SERVICE_PARAM_PASSWORD]);
  75. $msg = 'Logged In';
  76. $this->tLog->debug('Logged In');
  77. $response->addMessage('action', $msg);
  78. }
  79. catch (PermissionException $e) {
  80. return new ErrorResponse($response->getServiceName(),$response->getType(),$e);
  81. }
  82. return $response;
  83. }
  84. public function login($username,$password) {
  85. //Load user by username
  86. //$user = new User($username,'username');
  87. $user = new User(false);
  88. $user->d_username = $username;
  89. if ($user->match() === FALSE) {
  90. $msg = 'User with username('.$username.') does not exist.';
  91. $this->tLog->warn($msg);
  92. throw new PermissionException($msg,PermissionException::CODE_UNKNOWN_USER);
  93. }
  94. //Check the password
  95. if(!$user->isCorrectPassword($password)) {
  96. $msg = 'Incorrect password for User('.$username.')';
  97. $this->tLog->warn($msg);
  98. throw new PermissionException($msg,PermissionException::CODE_INCORRECT_PASSWORD);;
  99. }
  100. //Associate the user with the session
  101. $this->session->setCurrentUser($user);
  102. }
  103. function logoutRequest(ServiceResponse &$response) {
  104. $this->logout();
  105. $response->addMessage('action', 'Logged out');
  106. return $response;
  107. }
  108. public function logout() {
  109. $this->session->setCurrentUser();
  110. }
  111. function changePasswordRequest(ServiceResponse &$response,$params) {
  112. try {
  113. $this->changePassword($params[self::CHANGE_PASSWORD_SERVICE_PARAM_PASSWORD], $params[self::CHANGE_PASSWORD_SERVICE_PARAM_NEW_PASSWORD],$params[self::CHANGE_PASSWORD_SERVICE_PARAM_NEW_PASSWORD_CONFIRM]);
  114. $msg = 'Password Successfully Changed.';
  115. $this->tLog->debug($msg);
  116. $response->addMessage('action', $msg);
  117. }
  118. catch (PermissionException $e) {
  119. return new ErrorResponse($response->getServiceName(),$response->getType(),$e);
  120. }
  121. return $response;
  122. }
  123. public function changePassword($password,$newPassword,$newPasswordConfirm) {
  124. if ($this->isLoggedIn()) {
  125. $user = $this->session->getCurrentUser();
  126. $msg = $user->setPassword($password, $newPassword, $newPasswordConfirm);
  127. if ($msg !== "") {
  128. throw new PermissionException($msg,PermissionException::CODE_PASSWORD_CHANGE_ERROR);
  129. }
  130. $user->save();
  131. }
  132. else {
  133. $msg = 'Not logged in. Please login before updating the password.';
  134. throw new PermissionException($msg,PermissionException::CODE_PERMISSION_DENIED);
  135. }
  136. }
  137. function forgotPasswordRequest(ServiceResponse &$response,$params) {
  138. try {
  139. $this->forgotPassword($params[self::FORGOT_PASSWORD_SERVICE_PARAM_USERNAME]);
  140. $msg = 'Change Password Request sent to: '.$params[self::FORGOT_PASSWORD_SERVICE_PARAM_USERNAME];
  141. $this->tLog->debug($msg);
  142. $response->addMessage('action', $msg);
  143. }
  144. catch (PermissionException $e) {
  145. return new ErrorResponse($response->getServiceName(),$response->getType(),$e);
  146. }
  147. return $response;
  148. }
  149. function forgotPassword($username) {
  150. try {
  151. $user = new User(false);
  152. $user->d_username = $username;
  153. if ($user->match() == FALSE) {
  154. $msg = 'User with username('.$username.') does not exist.';
  155. $this->tLog->warn($msg);
  156. throw new PermissionException($msg,PermissionException::CODE_UNKNOWN_USER);
  157. }
  158. else {
  159. //User exists. Email them a new password
  160. $randString = md5('salt'.$_SERVER['REQUEST_TIME']);
  161. //Get the first few characters of the hash
  162. $newPassword = substr($randString, 0, 8);
  163. $user->resetPassword($newPassword);
  164. //Email the user their new password
  165. /**
  166. * Post a request to the email service
  167. * Fields:
  168. * - userName
  169. * - password
  170. */
  171. require_once 'includes/services/EmailHandler.php';
  172. $params = array(
  173. 'formName' => EmailHandler::FORGOT_PASSWORD_FORM,
  174. 'd_uri' => '_ADMIN_FORGOT_PASSWORD_',
  175. 'userName' => $user->d_username,
  176. 'password' => $newPassword
  177. );
  178. $emailHandler = new EmailHandler($params);
  179. $user->save();
  180. }
  181. }
  182. catch (PermissionException $e) {
  183. return new ErrorResponse($response->getServiceName(),$response->getType(),$e);
  184. }
  185. }
  186. /**
  187. * Method to generate a random password
  188. * Note: This is currently not being used.
  189. */
  190. function generateRandomPassword() {
  191. $aNumber = time();
  192. $randomString = md5($aNumber.'_'.rand());
  193. }
  194. public function isLoggedIn() {
  195. return !DBObject::isNull($this->session->getCurrentUser());
  196. }
  197. public function isAdmin() {
  198. return $this->isLoggedIn() && $this->session->d_o_user->isAdmin();
  199. }
  200. public function getLoggedInUser() {
  201. return $this->session->d_o_user;
  202. }
  203. public function requiresAdmin() {
  204. if (!$this->isLoggedIn()) {
  205. $msg = 'Permission Denied. Not logged in.';
  206. throw new PermissionException($msg,PermissionException::CODE_PERMISSION_DENIED);
  207. }
  208. else {
  209. return true;
  210. }
  211. }
  212. }
  213. ?>