Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace appcontrollers;
- use Yii;
- use yiifiltersAccessControl;
- use yiiwebController;
- use yiiwebResponse;
- use yiifiltersVerbFilter;
- use appmodelsLoginForm;
- use appmodelsContactForm;
- use appmodelsSignupForm;
- use appmodelsUser;
- use yiibaseConfigurable;
- class SiteController extends Controller
- {
- /**
- * {@inheritdoc}
- */
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::className(),
- 'only' => ['logout'],
- 'rules' => [
- [
- 'actions' => ['logout'],
- 'allow' => true,
- 'roles' => ['@'],
- ],
- ],
- ],
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- 'logout' => ['post'],
- ],
- ],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function actions()
- {
- return [
- 'error' => [
- 'class' => 'yiiwebErrorAction',
- ],
- 'captcha' => [
- 'class' => 'yiicaptchaCaptchaAction',
- 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
- ],
- ];
- }
- /**
- * Displays homepage.
- *
- * @return string
- */
- public function actionIndex()
- {
- return $this->render('index');
- }
- /**
- * Login action.
- *
- * @return Response|string
- */
- public function actionLogin()
- {
- if (!Yii::$app->user->isGuest) {
- return $this->goHome();
- }
- $model = new LoginForm();
- if ($model->load(Yii::$app->request->post()) && $model->login()) {
- return $this->goBack();
- }
- $model->password = '';
- return $this->render('login', [
- 'model' => $model,
- ]);
- }
- /**
- * Logout action.
- *
- * @return Response
- */
- public function actionLogout()
- {
- Yii::$app->user->logout();
- return $this->goHome();
- }
- /**
- * Displays contact page.
- *
- * @return Response|string
- */
- public function actionContact()
- {
- $model = new ContactForm();
- if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
- Yii::$app->session->setFlash('contactFormSubmitted');
- return $this->refresh();
- }
- return $this->render('contact', [
- 'model' => $model,
- ]);
- }
- /**
- * Displays about page.
- *
- * @return string
- */
- public function actionAbout()
- {
- return $this->render('about');
- }
- public function actionSignup(){
- if (!Yii::$app->user->isGuest) {
- return $this->goHome();
- }
- $model = new SignupForm();
- if($model->load(Yii::$app->request->post()) && $model->validate()){
- $user = new User();
- $user->username = $model->username;
- $user->password = Yii::$app->security->generatePasswordHash($model->password);
- if($user->save()){
- return $this->goHome();
- }
- }
- return $this->render('signup', compact('model'));
- }
- }
- namespace appmodels;
- use Yii;
- use yiibaseModel;
- /**
- * LoginForm is the model behind the login form.
- *
- * @property User|null $user This property is read-only.
- *
- */
- class LoginForm extends Model
- {
- public $username;
- public $password;
- public $rememberMe = true;
- private $_user = false;
- /**
- * @return array the validation rules.
- */
- public function rules()
- {
- return [
- // username and password are both required
- [['username', 'password'], 'required'],
- // rememberMe must be a boolean value
- ['rememberMe', 'boolean'],
- // password is validated by validatePassword()
- ['password', 'validatePassword'],
- ];
- }
- /**
- * Validates the password.
- * This method serves as the inline validation for password.
- *
- * @param string $attribute the attribute currently being validated
- * @param array $params the additional name-value pairs given in the rule
- */
- public function validatePassword($attribute, $params)
- {
- if (!$this->hasErrors()) {
- $user = $this->getUser();
- if (!$user || !$user->validatePassword($this->password)) {
- $this->addError($attribute, 'Incorrect username or password.');
- }
- }
- }
- /**
- * Logs in a user using the provided username and password.
- * @return bool whether the user is logged in successfully
- */
- public function login()
- {
- if ($this->validate()) {
- return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
- }
- return false;
- }
- /**
- * Finds user by [[username]]
- *
- * @return User|null
- */
- public function getUser()
- {
- if ($this->_user === false) {
- $this->_user = User::findByUsername($this->username);
- }
- return $this->_user;
- }
- }
- <?php
- namespace appmodels;
- use Yii;
- use yiibaseModel;
- use yiidbActiveRecord;
- use yiiwebIdentityInterface;
- class User extends ActiveRecord{
- public static function findIdentity($id)
- {
- return static::findOne($id);
- }
- public static function findByUsername($username)
- {
- return static::findOne($username);
- }
- public function validatePassword($password)
- {
- return Yii::$app->security->validatePassword($password);
- }
- }
Add Comment
Please, Sign In to add comment