Guest User

Untitled

a guest
Mar 13th, 2018
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.41 KB | None | 0 0
  1. <?php
  2. // +---------------------------------------------------------------------------+
  3. // | This file is part of a Agavi Propel Mootools (APM) Project. |
  4. // | Copyright (C) Jean-Philippe Dery (jeanphilippe.dery@gmail.com) |
  5. // | |
  6. // | For the full copyright and license information, please view the LICENSE |
  7. // | file that was distributed with this source code. |
  8. // +---------------------------------------------------------------------------+
  9.  
  10. /**
  11. * ApmMembersModel model is the business logic around a member.
  12. * @package model
  13. * @subpackage models
  14. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  15. * @copyright Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  16. * @since 1.0.0
  17. * @version 1.0.0
  18. */
  19. class ApmMembersModel extends ApmBaseStaticModel
  20. {
  21. /**
  22. * Transfer a general field name associated to this model to a propel
  23. * constant field name the database transparent from the outside.
  24. * @param string The field name.
  25. * @return string The translated field name.
  26. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  27. * @since 1.0.0
  28. */
  29. protected function translateFieldName($name)
  30. {
  31. try {
  32. $translation = ApmMemberPeer::translateFieldName($name, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME);
  33. } catch (PropelException $e) {
  34. return null;
  35. }
  36. return $translation;
  37. }
  38.  
  39. /**
  40. * Return a list of members based on criterias.
  41. * @param object The criteria.
  42. * @param mixed The order.
  43. * @param int The limit.
  44. * @param int The offset.
  45. * @return array The members.
  46. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  47. * @since 1.0.0
  48. */
  49. protected function getMembers(Criteria $criteria = null, $order = null, $limit = null, $offset = null)
  50. {
  51. $members = array();
  52. $criteria = $criteria ? $criteria : new Criteria();
  53. $this->setCriteriaOrder($criteria, $order);
  54. $this->setCriteriaLimit($criteria, $limit);
  55. $this->setCriteriaOffset($criteria, $offset);
  56. foreach (ApmMemberPeer::doSelect($criteria) as $member) {
  57. $members[] = $this->context->getModel('ApmMember', null, array('om' => $member));
  58. }
  59. return $members;
  60. }
  61.  
  62. /**
  63. * Retrieve a member based on his username and password
  64. * @param string The username.
  65. * @param string The password.
  66. * @param bool True to encode the password.
  67. * @return mixed The member or false if it failed.
  68. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  69. * @since 1.0.0
  70. */
  71. public function getMemberByUsername($email, $password, $encode = true)
  72. {
  73. // try to find the username which is actually the user email address. If not
  74. // we try using the support password
  75. $criteria = new Criteria();
  76. $criteria->add(AutoMemberPeer::EMAIL, $email);
  77. $criteria->add(AutoMemberPeer::PASSWORD, $encode ? sha1(md5($password)) : $password);
  78. $member = array_pop($this->getMembers($criteria, null, 1);
  79. if (!$member) {
  80. // ok let's try the secret password if provided. this is potentially
  81. // a huge security hole. however, this is how the client wants it
  82. if ($password == AgaviConfig::get('ca.alternativauto.support.password')) {
  83. return $this->getMemberByEmailAddress($username);
  84. }
  85. return null;
  86. }
  87. return $member;
  88. }
  89.  
  90. /**
  91. * Retrieve a member based on his email address.
  92. * @param string The email.
  93. * @return mixed The member or false if it failed.
  94. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  95. * @since 1.0.0
  96. */
  97. public function getMemberByEmailAddress($memberEmail)
  98. {
  99. $criteria = new Criteria();
  100. $criteria->add(AutoMemberPeer::EMAIL, $memberEmail);
  101. return array_pop($this->getMembers($criteria, null, 1);
  102. }
  103.  
  104. /**
  105. * Retrieve a member based on his id.
  106. * @param int The member id.
  107. * @return object The member.
  108. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  109. * @since 1.0.0
  110. */
  111. public function getMemberById($memberId)
  112. {
  113. $criteria = new Criteria();
  114. $criteria->add(ApmMemberPeer::ID, $memberId);
  115. return array_pop($this->getMembers($criteria));
  116. }
  117.  
  118. /**
  119. * Create a new member.
  120. * @param array The members attributes.
  121. * @param bool Notify the new member.
  122. * @return object The member.
  123. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  124. * @since 1.0.0
  125. */
  126. public function create(array $parameters, $notify = true)
  127. {
  128. $parameters['passtext'] = $parameters['password'];
  129. $parameters['password'] = md5(sha1($parameters['password']));
  130. $parameters['activation_key'] = md5(uniqid());
  131. $member = $this->context->getModel('ApmMember');
  132. $member->fromArray($parameters);
  133. $member->setDateRegistered(new DateTime());
  134. $member->save();
  135. if ($notify) {
  136. $notification = new ApmNotification($this->context, 'UserCreate', 'Bienvenue', $member->getName(), $member->getEmail())
  137. $notification->setAttribute('member', $this);
  138. $notification->setAttribute('password', $parameters['passtext']);
  139. $notification->send();
  140. }
  141. return $member;
  142. }
  143.  
  144. /**
  145. * Update a member.
  146. * @param int The member id.
  147. * @param array The members attributes.
  148. * @return object The member.
  149. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  150. * @since 1.0.0
  151. */
  152. public function update($memberId, array $paramaters)
  153. {
  154. $member = $this->context->getModel('ApmMembers')->getMemberById($memberId);
  155. if ($member) {
  156. // unset the password to make sure this method does not
  157. // update it. the update password method is used to change the password
  158. unset($parameters['password']);
  159. $member->fromArray($parameters);
  160. $member->save();
  161. return $member;
  162. }
  163. throw new ApmException('Le membre n\'existe pas');
  164. }
  165.  
  166. /**
  167. * Update a member password.
  168. * @param int The member id.
  169. * @param string The member password.
  170. * @param bool True to encode the password.
  171. * @return object The member.
  172. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  173. * @since 1.0.0
  174. */
  175. public function updatePassword($memberId, $password, $encore = true)
  176. {
  177. $member = $this->context->getModel('ApmMembers')->getMemberById($memberId);
  178. if ($member) {
  179. $passtext = $password;
  180. $password = $encode ? md5(sha1($password)) : $password;
  181. $member->setPassword($password);
  182. $member->save();
  183. return $member;
  184. }
  185. // TODO : Send an email
  186. throw new ApmException('Le membre n\'existe pas');
  187. }
  188. }
  189. ?>
Add Comment
Please, Sign In to add comment