Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class User extends CActiveRecord
- {
- public $newPassword;
- public $encryption_key;
- private $_public_display;
- ###############################
- ### Yii Generated Functions ###
- ###############################
- public static function model($className=__CLASS__)
- {
- return parent::model($className);
- }
- public function tableName()
- {
- return 'tbl_user';
- }
- public function beforeSave() {
- if(!empty($this->newPassword)) {
- $this->password = hash('sha256', $this->newPassword);
- }
- return true;
- }
- public function rules()
- {
- return array(
- array('username, newPassword', 'length', 'max'=>100),
- array('newPassword, encryption_key', 'safe'),
- array('email', 'email'),
- array('id, username, password, email, time_created, number_of_invites, activation_code, activated', 'safe', 'on'=>'search'),
- );
- }
- public function relations()
- {
- return array(
- 'profile' => array(self::HAS_ONE, 'Profile', 'user_id'),
- 'bookmarks' => array(self::HAS_MANY, 'Bookmark', 'user_id'),
- 'badges' => array(self::HAS_MANY, 'UserBadge', 'user_id'),
- 'social' => array(self::HAS_ONE, 'SocialNames', 'user_id'),
- );
- }
- public function attributeLabels()
- {
- return array(
- 'id' => 'ID',
- 'username' => 'Username',
- 'newPassword' => 'New Password',
- 'email' => 'Email',
- 'time_created' => 'Time Created',
- 'number_of_invites' => 'Number Of Invites',
- 'activation_code' => 'Activation Code',
- 'activated' => 'Activated',
- 'new_secretStash' => 'New Secret Stash Password',
- 'new_secretStashC' => 'Confirm Secret Stash Password',
- 'IV' => 'Encryption IV',
- 'encryption_key' => 'Bookmark Key',
- 'invite_code' => 'Invitation Code',
- );
- }
- public function search()
- {
- $criteria=new CDbCriteria;
- $criteria->compare('id',$this->id);
- $criteria->compare('username',$this->username,true);
- $criteria->compare('password',$this->password,true);
- $criteria->compare('email',$this->email,true);
- $criteria->compare('time_created',$this->time_created,true);
- $criteria->compare('number_of_invites',$this->number_of_invites);
- $criteria->compare('activation_code',$this->activation_code,true);
- $criteria->compare('activated',$this->activated);
- return new CActiveDataProvider(get_class($this), array(
- 'criteria'=>$criteria,
- ));
- }
- ############################
- ### User Model Functions ###
- ############################
- /*
- * Reset the password.
- */
- public function resetPassword() {
- $forgotten_password = new ForgottenPassword();
- $forgotten_password->user_id = $this->id;
- $forgotten_password->new_password = substr(md5(time()), 15);
- $forgotten_password->code = substr(md5(time() * 60), 15);
- $forgotten_password->save();
- $this->sendPasswordEmail($forgotten_password);
- }
- /*
- * Send the actual new password to the e-mail.
- */
- public function sendNewPasswordEmail($forgotten_password) {
- $user = $this;
- $username = $user->username;
- $email = $user->email;
- $user->password = hash('sha256', $forgotten_password->new_password);
- $user->save();
- $template = <<<HTML
- Dear {username},
- Below is your new password for use at {app}. We recommend you change this after logging in for the first time.
- <table>
- <tr>
- <td><strong>Username:</strong></td>
- <td>{username}</td>
- </tr>
- <tr>
- <td><strong>New Password:</strong></td>
- <td>{password}</td>
- </tr>
- </table><br /><br />
- Thank you,<br />
- The {app} team.
- HTML;
- $template = str_replace("{app}", Yii::app()->name, $template);
- $template = str_replace("{username}", $username, $template);
- $template = str_replace("{password}", $forgotten_password->new_password, $template);
- $forgotten_password->delete();
- try {
- Postmark::compose()
- ->addTo($email, $username)
- ->subject(Yii::app()->name . ' - New Password')
- ->messageHtml($template)
- ->send();
- } catch(Exception $ex) {
- }
- }
- /*
- * Send password reset e-mail.
- */
- public function sendPasswordEmail($forgotten_password) {
- $user = $this;
- $username = $user->username;
- $email = $user->email;
- $reset_password_url = Yii::app()->params->siteUrl . "user/forgotPassword?code=" . $forgotten_password->code;
- $template = <<<HTML
- Dear {username},
- You have requested a new password at {app}. Please click the link below to have your new password emailed to you.
- <table>
- <tr>
- <td><strong>Username:</strong></td>
- <td>{username}</td>
- </tr>
- <tr>
- <td><strong>Reset Link:</strong></td>
- <td>{link}</td>
- </tr>
- </table><br /><br />
- Thank you,<br />
- The {app} team.
- HTML;
- $template = str_replace("{app}", Yii::app()->name, $template);
- $template = str_replace("{username}", $username, $template);
- $template = str_replace("{link}", $reset_password_url, $template);
- try {
- Postmark::compose()
- ->addTo($email, $username)
- ->subject(Yii::app()->name . ' - Forgotten Password')
- ->messageHtml($template)
- ->send();
- } catch(Exception $ex) {
- }
- }
- /*
- * Sends activation e-mail.
- */
- public function sendActivationEmail() {
- $user = $this;
- $username = $user->username;
- $email = $user->email;
- $activation_code = $user->activation_code;
- $activation_url = Yii::app()->params->siteUrl . 'user/activate?code=' . $activation_code;
- $template = <<<HTML
- Thank you {username} for registering at {app}!<br /><br />
- Before we enable all of the features that {app} has to offer, we need you to first confirm your email address for us.<br /><br />
- This e-mail is <strong>VERY</strong> important. If you have chosen the "Grazely Managed" Bookmark Key, this e-mail (activation code) is the <strong><u>ONLY</u></strong> way to recover it.<br /><br />
- <table>
- <tr>
- <td><strong>Username:</strong></td>
- <td>{username}</td>
- </tr>
- <tr>
- <td><strong>Activation Code:</strong></td>
- <td>{activation_code}</td>
- </tr>
- <tr>
- <td><strong>Activation Link:</strong></td>
- <td>{activation_url}</td>
- </tr>
- </table><br /><br />
- Thank you,<br />
- The {app} team.
- HTML;
- $template = str_replace("{app}", Yii::app()->name, $template);
- $template = str_replace("{username}", $username, $template);
- $template = str_replace("{activation_code}", $activation_code, $template);
- $template = str_replace("{activation_url}", $activation_url, $template);
- try {
- Postmark::compose()
- ->addTo($email, $username)
- ->subject(Yii::app()->name . ' - User Activation')
- ->messageHtml($template)
- ->send();
- } catch(Exception $ex) {
- }
- }
- /*
- * Determines a user's public display name.
- */
- public function publicDisplay() {
- if(!empty($this->_public_display)) {
- return $this->_public_display;
- }
- $first_name = $this->profile->first_name;
- $last_name = $this->profile->last_name;
- if(!empty($first_name) && !empty($last_name)) {
- $this->_public_display = $first_name . ' ' . $last_name;
- return $this->_public_display;
- }
- if(!empty($first_name)) {
- $this->_public_display = ($first_name != $this->username) ? $first_name . ' (' . $this->username . ')' : $first_name;
- return $this->_public_display;
- }
- $this->_public_display = $this->username;
- return $this->username;
- }
- public function generateBookmarklet() {
- $template = <<<HTML
- <a id="bookmarklet_link" href="javascript:(function(){ function get_text%28%29 {var text %3D %27%27%3Bif%28window.getSelection%28%29%29 {text %3D window.getSelection%28%29%3B} else if%28document.getSelection%29 {text %3D document.getSelection%28%29%3B} else if%28document.selection%29 {text %3D document.selection.createRange%28%29.text%3B} else {return%3B}if%28text %3D%3D %27%27%29 {return false%3B} else {return text%3B}}var text %3D get_text%28%29%3Bif%28text %3D%3D false%29 {text %3D %27%27%3B}var api_key %3D %27{api_key}%27%3Bvar title %3D document.title%3Bvar url %3D window.location.href%3Bvar %24%24%24ftw11425%3Dwindow.open%28%27{site_url}bookmark/widget%3Fapi_key%3D%27+api_key+%27%26title%3D%27+title+%27%26url%3D%27+url+%27%26text%3D%27 + text%2C%27grazley%27%2C%27width%3D520%2Cheight%3D600%2Cscrollbars%3Dyes%2Ctoolbar%3Dno%2Cstatus%3Dno%2Clocation%3Dno%2Cmenubar%3Dno%2Cresizable%3Dyes%27%29%3B%24%24%24ftw11425.focus%28%29})();">
- Grazely!
- </a>
- HTML;
- $template = str_replace('{api_key}', APIKey::model()->findByAttributes(array("user_id" => Yii::app()->user->id))->api_key, $template);
- $template = str_replace('{site_url}', Yii::app()->params->siteUrl, $template);
- return $template;
- }
- /*
- * Determines if a user has access to encryption.
- */
- public function hasEncryption() {
- if(!empty(Yii::app()->user->encryption_key) && empty(Yii::app()->user->encryption_disabled)) {
- return true;
- } else {
- return false;
- }
- }
- /*
- * Returns the model of the current user.
- */
- public function currentUser() {
- return $this->findByPk(Yii::app()->user->id);
- }
- public function giveBadge($badge_id) {
- $user_badge = new UserBadge();
- $user_badge->user_id = $this->id;
- $user_badge->badge_id = $badge_id;
- $user_badge->save();
- }
- public function hasBadge($badge_id) {
- $user_badge = UserBadge::model()->findByAttributes(array('badge_id' => $badge_id, 'user_id' => $this->id));
- if($user_badge == null) {
- return false;
- } else {
- return true;
- }
- }
- }
Add Comment
Please, Sign In to add comment