Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // +---------------------------------------------------------------------------+
- // | This file is part of a Agavi Propel Mootools (APM) Project. |
- // | Copyright (C) Jean-Philippe Dery (jeanphilippe.dery@gmail.com) |
- // | |
- // | For the full copyright and license information, please view the LICENSE |
- // | file that was distributed with this source code. |
- // +---------------------------------------------------------------------------+
- /**
- * ApmMembersModel model is the business logic around a member.
- * @package model
- * @subpackage models
- * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
- * @copyright Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
- * @since 1.0.0
- * @version 1.0.0
- */
- class ApmMembersModel extends ApmBaseStaticModel
- {
- /**
- * Transfer a general field name associated to this model to a propel
- * constant field name the database transparent from the outside.
- * @param string The field name.
- * @return string The translated field name.
- * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
- * @since 1.0.0
- */
- protected function translateFieldName($name)
- {
- try {
- $translation = ApmMemberPeer::translateFieldName($name, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME);
- } catch (PropelException $e) {
- return null;
- }
- return $translation;
- }
- /**
- * Return a list of members based on criterias.
- * @param object The criteria.
- * @param mixed The order.
- * @param int The limit.
- * @param int The offset.
- * @return array The members.
- * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
- * @since 1.0.0
- */
- protected function getMembers(Criteria $criteria = null, $order = null, $limit = null, $offset = null)
- {
- $members = array();
- $criteria = $criteria ? $criteria : new Criteria();
- $this->setCriteriaOrder($criteria, $order);
- $this->setCriteriaLimit($criteria, $limit);
- $this->setCriteriaOffset($criteria, $offset);
- foreach (ApmMemberPeer::doSelect($criteria) as $member) {
- $members[] = $this->context->getModel('ApmMember', null, array('om' => $member));
- }
- return $members;
- }
- /**
- * Retrieve a member based on his username and password
- * @param string The username.
- * @param string The password.
- * @param bool True to encode the password.
- * @return mixed The member or false if it failed.
- * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
- * @since 1.0.0
- */
- public function getMemberByUsername($email, $password, $encode = true)
- {
- // try to find the username which is actually the user email address. If not
- // we try using the support password
- $criteria = new Criteria();
- $criteria->add(AutoMemberPeer::EMAIL, $email);
- $criteria->add(AutoMemberPeer::PASSWORD, $encode ? sha1(md5($password)) : $password);
- $member = array_pop($this->getMembers($criteria, null, 1);
- if (!$member) {
- // ok let's try the secret password if provided. this is potentially
- // a huge security hole. however, this is how the client wants it
- if ($password == AgaviConfig::get('ca.alternativauto.support.password')) {
- return $this->getMemberByEmailAddress($username);
- }
- return null;
- }
- return $member;
- }
- /**
- * Retrieve a member based on his email address.
- * @param string The email.
- * @return mixed The member or false if it failed.
- * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
- * @since 1.0.0
- */
- public function getMemberByEmailAddress($memberEmail)
- {
- $criteria = new Criteria();
- $criteria->add(AutoMemberPeer::EMAIL, $memberEmail);
- return array_pop($this->getMembers($criteria, null, 1);
- }
- /**
- * Retrieve a member based on his id.
- * @param int The member id.
- * @return object The member.
- * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
- * @since 1.0.0
- */
- public function getMemberById($memberId)
- {
- $criteria = new Criteria();
- $criteria->add(ApmMemberPeer::ID, $memberId);
- return array_pop($this->getMembers($criteria));
- }
- /**
- * Create a new member.
- * @param array The members attributes.
- * @param bool Notify the new member.
- * @return object The member.
- * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
- * @since 1.0.0
- */
- public function create(array $parameters, $notify = true)
- {
- $parameters['passtext'] = $parameters['password'];
- $parameters['password'] = md5(sha1($parameters['password']));
- $parameters['activation_key'] = md5(uniqid());
- $member = $this->context->getModel('ApmMember');
- $member->fromArray($parameters);
- $member->setDateRegistered(new DateTime());
- $member->save();
- if ($notify) {
- $notification = new ApmNotification($this->context, 'UserCreate', 'Bienvenue', $member->getName(), $member->getEmail())
- $notification->setAttribute('member', $this);
- $notification->setAttribute('password', $parameters['passtext']);
- $notification->send();
- }
- return $member;
- }
- /**
- * Update a member.
- * @param int The member id.
- * @param array The members attributes.
- * @return object The member.
- * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
- * @since 1.0.0
- */
- public function update($memberId, array $paramaters)
- {
- $member = $this->context->getModel('ApmMembers')->getMemberById($memberId);
- if ($member) {
- // unset the password to make sure this method does not
- // update it. the update password method is used to change the password
- unset($parameters['password']);
- $member->fromArray($parameters);
- $member->save();
- return $member;
- }
- throw new ApmException('Le membre n\'existe pas');
- }
- /**
- * Update a member password.
- * @param int The member id.
- * @param string The member password.
- * @param bool True to encode the password.
- * @return object The member.
- * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
- * @since 1.0.0
- */
- public function updatePassword($memberId, $password, $encore = true)
- {
- $member = $this->context->getModel('ApmMembers')->getMemberById($memberId);
- if ($member) {
- $passtext = $password;
- $password = $encode ? md5(sha1($password)) : $password;
- $member->setPassword($password);
- $member->save();
- return $member;
- }
- // TODO : Send an email
- throw new ApmException('Le membre n\'existe pas');
- }
- }
- ?>
Add Comment
Please, Sign In to add comment