Advertisement
Guest User

Untitled

a guest
Jul 4th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.20 KB | None | 0 0
  1. use Yii;
  2. use appmodelsauthLogin;
  3.  
  4. class UserController extends yiiwebController
  5. {
  6. //Экшен по умолчанию
  7. public $defaultAction = 'login';
  8.  
  9.  
  10. public function beforeAction($action) {
  11. ///////////////////////////////////////////////////////////////
  12. //FOR DEBUG
  13. $this->enableCsrfValidation = false;
  14. return parent::beforeAction($action);
  15. //////////////////////////////////////////////////////////////
  16. }
  17.  
  18. public function actionLogin()
  19. {
  20.  
  21.  
  22. if (!Yii::$app->user->isGuest) {
  23. //TODO
  24. }
  25.  
  26. $model = new Login();
  27. //Yii::$app->response->format = yiiwebResponse::FORMAT_JSON;
  28. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  29. print('ok');
  30. }
  31.  
  32.  
  33. return $this->render('login');
  34.  
  35.  
  36. }
  37.  
  38. public function actionRegister()
  39. {
  40. return $this->render('register');
  41. }
  42.  
  43. public function actionPassword()
  44. {
  45.  
  46. }
  47.  
  48. }
  49.  
  50. namespace appmodelsauth;
  51.  
  52. use Yii;
  53. use yiibaseModel;
  54.  
  55. class Login extends Model
  56. {
  57. public $email;
  58. public $phone;
  59. public $password;
  60. public $rememberMe = true;
  61. public $typeLogin;
  62. private $_user = false;
  63.  
  64.  
  65. public function rules()
  66. {
  67. return [
  68. [['password'], 'required'],
  69. ['email', 'validateLogin'],
  70. ['phone', 'validateLogin'],
  71. ['password', 'validatePassword'],
  72. ['rememberMe', 'boolean'],
  73. ];
  74. }
  75.  
  76.  
  77. /*
  78. * Валидация введеного адреса эл. почты или телефона
  79. * @return bool
  80. */
  81. public function validateLogin($model, $attribute)
  82. {
  83. //TODO
  84. if ($this->email) {
  85. $this->typeLogin = 'email';
  86. return;
  87. }
  88. if ($this->phone) {
  89. $this->typeLogin = 'phone';
  90. return;
  91. }
  92. $this->addError($attribute, 'Неправильное имя пользователя или пароль!');
  93. }
  94.  
  95. /*
  96. * Валидация пароля
  97. * @return bool
  98. */
  99. public function validatePassword($model, $attribute)
  100. {
  101. if ( !$this->hasErrors() ) {
  102. $user = $this->getUser();
  103. if (!$user || !$user->validatePassword($this->password)) {
  104. $this->addError($attribute, 'Неправильное имя пользователя или пароль!');
  105. }
  106. }
  107. }
  108.  
  109. /*
  110. * Авторизация пользователя
  111. * @return bool
  112. */
  113. public function login()
  114. {
  115. if ($this->validate()) {
  116. return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
  117. }
  118. return false;
  119. }
  120.  
  121.  
  122. /*
  123. * Поиск пользователя по логину или номкру телефона
  124. * @return bool
  125. */
  126. public function getUser()
  127. {
  128. if ($this->_user === false) {
  129. if ($this->typeLogin === 'email')
  130. $this->_user = User::findByEmail($this->email);
  131. else
  132. $this->_user = User::findByPhone($this->phone);
  133. }
  134.  
  135. return $this->_user;
  136. }
  137.  
  138. //public function load($data)
  139. //{
  140. // $this->email = isset($data['email']) ? $data['email'] : null;
  141. // $this->phone = isset($data['phone']) ? $data['phone'] : null;
  142. // $this->password = isset($data['password']) ? $data['password'] : null;
  143. // $this->rememberMe = isset($data['rememberMe']) ? $data['rememberMe'] : null;
  144. // parent::load($data);
  145. //}
  146.  
  147. }
  148.  
  149. <?php
  150.  
  151. namespace appmodelsauth;
  152.  
  153. use Yii;
  154. use yiidbActiveRecord;
  155. use yiiwebIdentityInterface;
  156. use yiibehaviorsTimestampBehavior;
  157.  
  158. /**
  159. * @property integer $id
  160. * @property string $email
  161. * @property string $phone
  162. * @property string $password
  163. * @property string $name
  164. * @property string $sename
  165. * @property string $fathername
  166. * @property integer $group_id
  167. * @property integer $enabled
  168. * @property string $last_ip
  169. * @property string $created
  170. * @property integer $subscribed_on_news
  171. * @property integer $downloaded_to_unisender
  172. */
  173. class User extends ActiveRecord implements IdentityInterface
  174. {
  175.  
  176.  
  177. private $salt = '79d6e182b3c811c559e6b';
  178.  
  179.  
  180. public static function tableName()
  181. {
  182. return 's_users';
  183. }
  184.  
  185.  
  186. public function rules()
  187. {
  188. return [
  189. [['email', 'phone', 'name', 'sename'], 'required'],
  190. [['group_id', 'enabled', 'subscribed_on_news', 'downloaded_to_unisender'], 'integer'],
  191. [['created'], 'safe'],
  192. [['email', 'password', 'name', 'fathername'], 'string', 'max' => 255],
  193. [['phone', 'sename'], 'string', 'max' => 30],
  194. [['last_ip'], 'string', 'max' => 15],
  195. [['phone'], 'unique'],
  196. ];
  197. }
  198.  
  199.  
  200. public function attributeLabels()
  201. {
  202. return [
  203. 'id' => 'ID',
  204. 'email' => 'Email',
  205. 'phone' => 'Phone',
  206. 'password' => 'Password',
  207. 'name' => 'Name',
  208. 'sename' => 'Sename',
  209. 'fathername' => 'Fathername',
  210. 'group_id' => 'Group ID',
  211. 'enabled' => 'Enabled',
  212. 'last_ip' => 'Last Ip',
  213. 'created' => 'Created',
  214. 'subscribed_on_news' => 'Subscribed On News',
  215. 'downloaded_to_unisender' => 'Downloaded To Unisender',
  216. ];
  217. }
  218.  
  219. /*
  220. * Поведения
  221. */
  222. public function behaviors()
  223. {
  224. return [
  225. TimestampBehavior::className()
  226. ];
  227. }
  228.  
  229. /*
  230. * Поиск пользователя по email
  231. * @param string $email
  232. * @return
  233. */
  234. public function findByEmail($email)
  235. {
  236. return static::findOne(['email' => $email ]);
  237. }
  238.  
  239. /*
  240. * Поиск пользователя по номеру телефона
  241. * @param string $email
  242. * @return
  243. */
  244. public function findByPhone($phone)
  245. {
  246. return static::findOne(['phone' => $phone ]);
  247. }
  248.  
  249.  
  250. /*
  251. * Проверка пароля
  252. * @parem string $password
  253. * @return bool
  254. */
  255. public function validatePassword($password)
  256. {
  257. return $this->password === md5($this->salt.$password.md5($password));
  258. }
  259.  
  260. /*
  261. * Добавление/Изменение пароля
  262. */
  263. public function setPassword($password)
  264. {
  265. $this->password = md5($this->salt.$password.md5($password));
  266. }
  267.  
  268. /*
  269. * Генерация случайной строки для индетификации пользователя,
  270. * например может понадобится в опции "запомнить меня"
  271. */
  272. public function generateAuthKey()
  273. {
  274. $this->authKey === $app->security->generateRandomString();
  275. }
  276.  
  277.  
  278. /* Реализация методов интерфейса IdentityInterface */
  279.  
  280.  
  281. public static function findIdentity($id)
  282. {
  283. return static::findOne($id);
  284. }
  285.  
  286. public static function findIdentityByAccessToken($token, $type = null)
  287. {
  288. //return static::findOne(['access_token' => $token]);
  289. }
  290.  
  291. public function getId()
  292. {
  293. return $this->id;
  294. }
  295.  
  296. public function getAuthKey()
  297. {
  298. return $this->authKey;
  299. }
  300.  
  301. public function validateAuthKey($authKey)
  302. {
  303. return $this->authKey === $authKey;
  304. }
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement