Ejemplo n.º 1
0
 /**
  * {@inheritdoc}
  *
  * @param string $module
  * @param string $controller
  * @param array $params
  * @return void
  */
 protected function preDispatch($module, $controller, $params = array())
 {
     // example of setup default title
     Layout::title("Bluz Skeleton");
     // apply "remember me" function
     if (!$this->user() && !empty($_COOKIE['rToken']) && !empty($_COOKIE['rId'])) {
         // try to login
         try {
             Auth\Table::getInstance()->authenticateCookie($_COOKIE['rId'], $_COOKIE['rToken']);
         } catch (AuthException $e) {
             $this->getResponse()->setCookie('rId', '', 1, '/');
             $this->getResponse()->setCookie('rToken', '', 1, '/');
         }
     }
     parent::preDispatch($module, $controller, $params);
 }
Ejemplo n.º 2
0
 /**
  * @param array $data
  * @throws Exception
  * @throws ValidatorException
  * @return integer
  */
 public function createOne($data)
 {
     // password
     $password = $data['password'] ?? null;
     $password2 = $data['password2'] ?? null;
     if (empty($password)) {
         throw ValidatorException::exception('password', __('Password can\'t be empty'));
     }
     if ($password !== $password2) {
         throw ValidatorException::exception('password2', __('Password is not equal'));
     }
     if ($data['id'] == '') {
         unset($data['id']);
     }
     /** @var $row Row */
     $row = $this->getTable()->create();
     $row->setFromArray($data);
     $row->status = Table::STATUS_PENDING;
     $row->save();
     $userId = $row->id;
     // create auth
     Auth\Table::getInstance()->generateEquals($row, $password);
     // create activation token
     // valid for 5 days
     $actionRow = UsersActions\Table::getInstance()->generate($userId, UsersActions\Table::ACTION_ACTIVATION, 5);
     // send activation email
     // generate activation URL
     $activationUrl = Router::getFullUrl('users', 'activation', ['code' => $actionRow->code, 'id' => $userId]);
     $subject = "Activation";
     $body = Application::getInstance()->dispatch('users', 'mail/template', ['template' => 'registration', 'vars' => ['user' => $row, 'activationUrl' => $activationUrl, 'password' => $password]])->render();
     try {
         $mail = Mailer::create();
         $mail->Subject = $subject;
         $mail->msgHTML(nl2br($body));
         $mail->addAddress($data['email']);
         Mailer::send($mail);
     } catch (\Exception $e) {
         Logger::log('error', $e->getMessage(), ['module' => 'users', 'controller' => 'change-email', 'userId' => $userId]);
         throw new Exception('Unable to send email. Please contact administrator.');
     }
     // show notification and redirect
     Messages::addSuccess("Your account has been created and an activation link has" . "been sent to the e-mail address you entered.<br/>" . "Note that you must activate the account by clicking on the activation link" . "when you get the e-mail before you can login.");
     // wtf?
     // redirectTo('index', 'index');
     return $userId;
 }
Ejemplo n.º 3
0
 /**
  * @var Users\Row $user
  */
 $user = Users\Table::findRow($userId);
 if (!$user) {
     throw new NotFoundException('User not found');
 }
 $this->assign('email', $user->email);
 if (Request::isPost()) {
     // process form
     try {
         if (empty($password)) {
             throw new Exception('Password is empty');
         }
         // login/password
         Auth\Table::getInstance()->checkEquals($user->login, $password);
         // check email for unique
         $emailUnique = Users\Table::findRowWhere(['email' => $email]);
         if ($emailUnique && $emailUnique->id != $userId) {
             throw new Exception('User with email "' . htmlentities($email) . '" already exists');
         }
         // generate change mail token and get full url
         $actionRow = UsersActions\Table::getInstance()->generate($userId, Table::ACTION_CHANGE_EMAIL, 5, ['email' => $email]);
         $changeUrl = Router::getFullUrl('users', 'change-email', ['token' => $actionRow->code]);
         $subject = __("Change email");
         $body = $this->dispatch('users', 'mail/template', ['template' => 'change-email', 'vars' => ['user' => $user, 'email' => $email, 'changeUrl' => $changeUrl, 'profileUrl' => Router::getFullUrl('users', 'profile')]])->render();
         try {
             $mail = Mailer::create();
             $mail->Subject = $subject;
             $mail->msgHTML(nl2br($body));
             $mail->addAddress($email);
Ejemplo n.º 4
0
 *   ),
 *   @SWG\Parameter(
 *       name="password",
 *       in="formData",
 *       description="Password",
 *       required=true,
 *       type="string"
 *   ),
 *   @SWG\Response(response=200, description="Token"),
 *   @SWG\Response(response=400, description="Login and password are required"),
 *   @SWG\Response(response=401, description="User not found")
 * )
 */
return function () {
    /**
     * @var Bootstrap $this
     */
    if ($this->getRequest()->isPost()) {
        $params = $this->getRequest()->getAllParams();
        if (!array_key_exists('login', $params) || !array_key_exists('password', $params)) {
            throw new BadRequestException('Login and password are required');
        }
        // try to authenticate
        $equalsRow = Auth\Table::getInstance()->checkEquals($params['login'], $params['password']);
        // create auth row with token
        $tokenRow = Auth\Table::getInstance()->generateToken($equalsRow);
        return ['token' => $tokenRow->token];
    } else {
        throw new NotImplementedException();
    }
};
Ejemplo n.º 5
0
    } elseif ($interval->invert) {
        Messages::addError('The activation code has expired');
        $actionRow->delete();
        Response::redirectTo('index', 'index');
    } else {
        $user = Users\Table::findRow($id);
        $this->assign('user', $user);
        $this->assign('code', $code);
        if (Request::isPost()) {
            try {
                if (empty($password) or empty($password2)) {
                    throw new Exception('Please enter your new password');
                }
                if ($password != $password2) {
                    throw new Exception('Please repeat your new password');
                }
                // remove old auth record
                if ($oldAuth = Auth\Table::getInstance()->getAuthRow(Auth\Table::PROVIDER_EQUALS, $user->login)) {
                    $oldAuth->delete();
                }
                // create new auth record
                Auth\Table::getInstance()->generateEquals($user, $password);
                // show notification and redirect
                Messages::addSuccess("Your password has been updated");
                Response::redirectTo('users', 'signin');
            } catch (Exception $e) {
                Messages::addError($e->getMessage());
            }
        }
    }
};
Ejemplo n.º 6
0
 if ($this->user()) {
     Messages::addNotice('Already signed');
     $this->redirectTo('index', 'index');
 } elseif (Request::isPost()) {
     try {
         if (empty($login)) {
             throw new Exception("Login is empty");
         }
         if (empty($password)) {
             throw new Exception("Password is empty");
         }
         // login/password
         // throw AuthException
         Auth\Table::getInstance()->authenticateEquals($login, $password);
         if ($rememberMe) {
             Auth\Table::getInstance()->generateCookie();
         }
         Messages::addNotice('You are signed');
         // try to rollback to previous called URL
         if ($rollback = Session::get('rollback')) {
             Session::delete('rollback');
             $this->redirect($rollback);
         }
         // try back to index
         $this->redirectTo('index', 'index');
     } catch (Exception $e) {
         Messages::addError($e->getMessage());
         $view->login = $login;
     } catch (AuthException $e) {
         Messages::addError($e->getMessage());
         $view->login = $login;
Ejemplo n.º 7
0
 /**
  * @return void
  */
 public function authProcess()
 {
     $this->authAdapter = $this->getAuthAdapter();
     $profile = $this->getProfile();
     /**
      * @var Auth\Table $authTable
      */
     $authTable = Auth\Table::getInstance();
     $auth = $authTable->getAuthRow(strtolower($this->providerName), $profile->identifier);
     if ($this->identity) {
         if ($auth) {
             Messages::addNotice(sprintf('You have already linked to %s', $this->providerName));
             $this->response->redirectTo('users', 'profile', ['id' => $this->identity->id]);
         } else {
             $user = Users\Table::findRow($this->identity->id);
             $this->registration($profile, $user);
         }
     }
     if ($auth) {
         $this->alreadyRegisteredLogic($auth);
     } else {
         Messages::addError(sprintf('First you need to be linked to %s', $this->providerName));
         $this->response->redirectTo('users', 'signin');
     }
 }
Ejemplo n.º 8
0
    $user = Users\Table::findRow($userId);
    if (!$user) {
        throw new NotFoundException('User not found');
    }
    if (Request::isPost()) {
        // process form
        try {
            if (empty($password)) {
                throw new BadRequestException('Please input current password');
            }
            if (empty($new_password)) {
                throw new BadRequestException('Please input new password');
            }
            if (empty($new_password2)) {
                throw new BadRequestException('Please repeat new password');
            }
            $authTable = Auth\Table::getInstance();
            // password check
            $authTable->checkEquals($user->login, $password);
            // create new Auth record
            $authTable->generateEquals($user, $new_password);
            Messages::addSuccess("The password was updated successfully");
            // try back to index
            $this->redirectTo('users', 'profile');
        } catch (BadRequestException $e) {
            Messages::addError($e->getMessage());
        } catch (AuthException $e) {
            Messages::addError($e->getMessage());
        }
    }
};
Ejemplo n.º 9
0
 * @param string $id
 *
 * @route /api/{$resource}
 * @param string $resource
 *
 * @return mixed
 */
return function ($resource, $id, $relation, $relationId) {
    /**
     * @var Controller $this
     */
    $this->useJson();
    Auth::clearIdentity();
    try {
        // authentication by api token
        if ($token = Request::getParam('token')) {
            Table::getInstance()->authenticateToken($token);
        }
        $params = [];
        foreach ([$id, $relation, $relationId] as $param) {
            if (!is_null($param)) {
                $params[] = $param;
            }
        }
        return $this->dispatch('api', 'resources/' . $resource, $params);
    } catch (\Exception $e) {
        // process exceptions here
        Response::setStatusCode($e->getCode());
        return ['error' => $e->getMessage()];
    }
};
Ejemplo n.º 10
0
<?php

/**
 * Logout proccess
 *
 * @author   Anton Shevchuk
 * @created  20.07.11 18:39
 * @return closure
 */
namespace Application;

use Application\Auth as AppAuth;
use Bluz\Controller\Controller;
use Bluz\Proxy\Auth;
use Bluz\Proxy\Messages;
use Bluz\Proxy\Response;
/**
 * @return void
 */
return function () {
    /**
     * @var Controller $this
     */
    if ($this->user()) {
        AppAuth\Table::getInstance()->removeCookieToken($this->user()->id);
        Auth::clearIdentity();
    }
    Messages::addNotice('You are signout');
    Response::redirectTo('index', 'index');
};