Source for file PersonObject.inc
Documentation is available at PersonObject.inc
require_once $config->get('sysRoot'). 'alpha/model/AlphaDAO.inc';
require_once $config->get('sysRoot'). 'alpha/model/RightsObject.inc';
require_once $config->get('sysRoot'). 'alpha/model/ArticleCommentObject.inc';
* The main person/user class for the site
* @author John Collins <dev@alphaframework.org>
* @version $Id: PersonObject.inc 1453 2011-12-04 15:12:54Z johnc $
* @license http://www.opensource.org/licenses/bsd-license.php The BSD License
* @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the
* following conditions are met:
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
* * Neither the name of the Alpha Framework nor the names
* of its contributors may be used to endorse or promote
* products derived from this software without specific
* prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* The forum display name of the person
* The email address for the person
* The password for the person
* A Relation containing all of the rights groups that this person belongs to
* An array of data display labels for the class properties
"displayName"=> "Display Name",
"email"=> "E-mail Address",
"state"=> "Account state",
"URL"=> "Your site address",
"rights"=> "Rights Group Membership");
* The name of the database table for the class
* The state of the person (account status)
* The website URL of the person
private static $logger = null;
* Constructor for the class that populates all of the complex types with default values
self::$logger = new Logger('PersonObject');
self::$logger->debug('>>__construct()');
// ensure to call the parent constructor
$this->displayName->setHelper('Please provide a name for display on the website (only letters, numbers, and .-_ characters are allowed!).');
$this->email->setSize(70);
$this->email->setHelper('Please provide a valid e-mail address as your username.');
$this->password->setHelper('Please provide a password for logging in.');
$this->state->setValue('Active');
$this->URL->setHelper('URLs must be in the format http://some_domain/ or left blank!');
// add unique keys to displayName and email (which is effectively the username in Alpha)
self::$logger->debug('<<__construct');
* Set up the transient attributes for the rights group after it has loaded
* Set up the transient attributes for the site after it has loaded
* Looks up the OID for the Standard rights group, then relates the new person
* to that group if they are not in it already. If that group does not exist it
$standardGroup = new RightsObject();
$standardGroup->loadByAttribute('name', 'Standard');
$standardGroup->set('name', 'Standard');
$lookup = $this->rights->getLookup();
$lookup->setValue(array($this->getID(), $standardGroup->getID()));
* Encrypts any fields called 'password' posted for the PersonObject
$_POST['password'] = crypt($_POST['password']);
* Sets up the Relation definitions on this BO
// set up MANY-TO-MANY relation person2rights
$this->rights->setRelatedClass('PersonObject', 'left');
$this->rights->setRelatedClassDisplayField('email', 'left');
$this->rights->setRelatedClass('RightsObject', 'right');
$this->rights->setRelatedClassDisplayField('name', 'right');
$this->rights->setRelationType('MANY-TO-MANY');
* @param string $displayName
* Checks to see if the person is in the rights group specified
* @param string $groupName
public function inGroup($groupName) {
if(self::$logger == null)
self::$logger = new Logger('PersonObject');
self::$logger->debug('>>inGroup(groupName=['. $groupName. '])');
$group = new RightsObject();
$group->loadByAttribute('name', $groupName);
self::$logger->error('Unable to load the group named ['. $groupName. ']');
self::$logger->debug('<<inGroup [false]');
$rel = $group->getMembers();
// load all person2rights RelationLookup objects for this person
$lookUps = $rel->getLookup()->loadAllByAttribute('leftID', $this->getID());
foreach($lookUps as $lookUp) {
// the rightID (i.e. RightsObject OID) will be on the right side of the value array
$ids = $lookUp->getValue();
// if we have found a match, return true right away
if($ids[1] == $group->getID()) {
self::$logger->debug('<<inGroup [true]');
self::$logger->debug('<<inGroup [false]');
self::$logger->debug('<<inGroup [false]');
* A generic method for mailing a person
* @throws MailNotSentException
public function sendMail($message, $subject) {
$body = '<html><head></head><body><p>Dear '. $this->getDisplayName(). ',</p>';
$body .= '</body></html>';
$headers = 'MIME-Version: 1.0'. "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1'. "\n";
$headers .= "From: ". $config->get('sysReplyTo'). "\n";
mail($this->get('email'), $subject, $body, $headers);
* Generates a random password for the user
$alphabet = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
// the password will be 7 random characters and 2 numbers
for ($i = 0; $i < 7; $i++ ) {
$newPassword.= $alphabet[rand(0,25)];
$newPassword.= rand(0,100);
$newPassword.= rand(0,100);
* Method for getting a count of the amount of article comments posted by the user
$sqlQuery = "SELECT COUNT(OID) AS post_count FROM ". $temp->getTableName(). " WHERE created_by='". $this->OID. "';";
$result = $this->query($sqlQuery);
throw new AlphaException('Failed to get the count of the comments posted for the person ['. $this->getDisplayName(). '], query is ['. $sqlQuery. ']');
if(isset ($row['post_count']))
return $row['post_count'];
|