1: <?php
2:
3: /*****************************************************************************************
4: * X2Engine Open Source Edition is a customer relationship management program developed by
5: * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
6: *
7: * This program is free software; you can redistribute it and/or modify it under
8: * the terms of the GNU Affero General Public License version 3 as published by the
9: * Free Software Foundation with the addition of the following permission added
10: * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11: * IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
12: * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13: *
14: * This program is distributed in the hope that it will be useful, but WITHOUT
15: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16: * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
17: * details.
18: *
19: * You should have received a copy of the GNU Affero General Public License along with
20: * this program; if not, see http://www.gnu.org/licenses or write to the Free
21: * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22: * 02110-1301 USA.
23: *
24: * You can contact X2Engine, Inc. P.O. Box 66752, Scotts Valley,
25: * California 95067, USA. or at email address contact@x2engine.com.
26: *
27: * The interactive user interfaces in modified source and object code versions
28: * of this program must display Appropriate Legal Notices, as required under
29: * Section 5 of the GNU Affero General Public License version 3.
30: *
31: * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32: * these Appropriate Legal Notices must retain the display of the "Powered by
33: * X2Engine" logo. If the display of the logo is not reasonably feasible for
34: * technical reasons, the Appropriate Legal Notices must display the words
35: * "Powered by X2Engine".
36: *****************************************************************************************/
37:
38: /**
39: * Form model for logging into the app.
40: *
41: * @package application.models
42: * @property UserIdentity $identity The user identity component for the current
43: * login.
44: * @propoerty User $user The user model corresponding to the current login; null
45: * if no match for username/alias was found.
46: */
47: class LoginForm extends X2FormModel {
48:
49: public $username;
50: public $password;
51: public $rememberMe;
52: public $verifyCode;
53: public $useCaptcha;
54: private $_identity;
55:
56: /**
57: * Validation rules for logins.
58: * @return array
59: */
60: public function rules() {
61: return array(
62: // username and password are required
63: array('username, password', 'required'),
64: // rememberMe needs to be a boolean
65: array('rememberMe', 'boolean'),
66: // password needs to be authenticated
67: array('password', 'authenticate'),
68: // captcha needs to be filled out
69: array(
70: 'verifyCode',
71: 'captcha',
72: 'allowEmpty' => !(CCaptcha::checkRequirements()), 'on' => 'loginWithCaptcha'),
73: array('verifyCode', 'safe'),
74: );
75: }
76:
77: /**
78: * Declares attribute labels.
79: * @return array
80: */
81: public function attributeLabels(){
82: return array(
83: 'username' => Yii::t('app', 'Username'),
84: 'password' => Yii::t('app', 'Password'),
85: 'rememberMe' => Yii::t('app', 'Remember me'),
86: 'verifyCode' => Yii::t('app', 'Verification Code'),
87: );
88: }
89:
90: /**
91: * Authenticates the password.
92: *
93: * This is the 'authenticate' validator as declared in rules().
94: * @param string $attribute Attribute name
95: * @param array $params validation parameters
96: */
97: public function authenticate($attribute, $params) {
98: if (!$this->hasErrors()) {
99: if (!$this->identity->authenticate()) {
100: if($this->identity->errorCode === UserIdentity::ERROR_DISABLED){
101: $this->addError('username',Yii::t('app','Login for that user account has been disabled.'));
102: $this->addError('password',Yii::t('app','Login for that user account has been disabled.'));
103: }else{
104: $this->addError('username', Yii::t('app', 'Incorrect username or password. Note, usernames are case sensitive.'));
105: $this->addError('password', Yii::t('app', 'Incorrect username or password. Note, usernames are case sensitive.'));
106: }
107: }
108: }
109: }
110:
111: /**
112: * Logs in the user using the given username and password in the model.
113: *
114: * @param boolean $google Whether or not Google is being used for the login
115: * @return boolean whether login is successful
116: */
117: public function login($google = false) {
118: if(!isset($this->_identity))
119: $this->getIdentity()->authenticate($google);
120: if($this->getIdentity()->errorCode === UserIdentity::ERROR_NONE) {
121: $duration = $this->rememberMe ? 2592000 : 0; //60*60*24*30 = 30 days
122: Yii::app()->user->login($this->_identity, $duration);
123:
124: // update lastLogin time
125: $user = User::model()->findByPk(Yii::app()->user->getId());
126: Yii::app()->setSuModel($user);
127: $user->lastLogin = $user->login;
128: $user->login = time();
129: $user->update(array('lastLogin','login'));
130:
131: Yii::app()->session['loginTime'] = time();
132:
133: return true;
134: }
135:
136: return false;
137: }
138:
139: /**
140: * User identity component.
141: *
142: * @return UserIdentity
143: */
144: public function getIdentity(){
145: if(!isset($this->_identity)){
146: $this->_identity = new UserIdentity($this->username, $this->password);
147: }
148: return $this->_identity;
149: }
150:
151: /**
152: * Returns the user model corresponding to the identity for the login
153: *
154: * @return User
155: */
156: public function getUser() {
157: return $this->getIdentity()->getUserModel();
158: }
159:
160: /**
161: * Resolves the correct username to use for login form security and sessions
162: *
163: * @return type
164: */
165: public function getSessionUserName() {
166: if((($user = $this->getUser()) instanceof User))
167: return $user->username;
168: return $this->username;
169: }
170:
171: }
172: