resurtm

Untitled

Oct 17th, 2015
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.40 KB | None | 0 0
  1. <?php
  2.  
  3. namespace frontend\controllers;
  4.  
  5. use Yii;
  6. use yii\web\Controller;
  7. use yii\web\HttpException;
  8. use yii\filters\AccessControl;
  9. use yii\filters\VerbFilter;
  10. use common\models\Order;
  11. use common\models\User;
  12.  
  13. /**
  14.  * Controller responsible for orders.
  15.  */
  16. class OrderController extends Controller
  17. {
  18.     /**
  19.      * @inheritdoc
  20.      */
  21.     public function behaviors()
  22.     {
  23.         return [
  24.             'access' => [
  25.                 'class' => AccessControl::className(),
  26.                 'only' => ['assign'],
  27.                 'rules' => [
  28.                     ['actions' => ['assign'], 'allow' => true, 'roles' => ['manager']],
  29.                 ],
  30.             ],
  31.             'verbs' => [
  32.                 'class' => VerbFilter::className(),
  33.                 'actions' => [
  34.                     'assign' => ['post'],
  35.                 ],
  36.             ],
  37.         ];
  38.     }
  39.  
  40.     /**
  41.      * @param integer $id of the order to be linked with the current manager user.
  42.      * @throws HttpException in case order cannot be found.
  43.      */
  44.     public function actionAssign($id)
  45.     {
  46.         /** @var Order $order */
  47.         if (($order = Order::findOne($id)) === null) {
  48.             throw new HttpException(404, 'Your order cannot be found!');
  49.         }
  50.  
  51.         /** @var User $manager */
  52.         $manager = User::findOne(Yii::$app->getUser()->getId());
  53.  
  54.         $order->apply($manager);
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment