Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yii-user + RESTFullYii #158

Open
djpaoloc opened this issue Sep 11, 2014 · 0 comments
Open

Yii-user + RESTFullYii #158

djpaoloc opened this issue Sep 11, 2014 · 0 comments

Comments

@djpaoloc
Copy link

Hi,
I'm try to implement RESTFullYii with Yii-user module.
This is my LoginController

class LoginController extends Controller
{
    public $defaultAction = 'login';

    /**
     * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
     * using two-column layout. See 'protected/views/layouts/column2.php'.
     */
    public $layout = '//layouts/column1';

// Uncomment the following methods and override them if needed
    public function filters()
    {
        // return the filter configuration for this controller, e.g.:
        return array(
            'accessControl', // perform access control for CRUD operations
            array(
                'ext.starship.RestfullYii.filters.ERestFilter + 
                REST.GET, REST.PUT, REST.POST, REST.DELETE'
            ),
        );
    }

   public function accessRules()
   {
       return array(
           array('allow', 'actions'=>array('REST.GET', 'REST.PUT', 'REST.POST', 'REST.DELETE', 'login'),
              'users'=>array('*'),
           ),
           array('deny',  // deny all users
              'users'=>array('*'),
           ),
       );
   }
    public function actions()
    {
        // return external action classes, e.g.:
        return array(
            'REST.' => 'ext.starship.RestfullYii.actions.ERestActionProvider',
        );
    }

    public function restEvents()
    {
         $this->onRest('post.filter.req.auth.ajax.user', function($validation) {
            return true;
        }); 

        $this->onRest('req.post.user.render', function($data) {
            echo CJSON::encode($data);
            //The $data should contain username and password.
            //Login the user here. Should be easy.
            //Then render a response 
        }); 

        $this->onRest('model.instance', function(){
            return new UserLogin(); 
        }); 
    }  

    /**
     * Displays the login page
     */
    public function actionLogin()
    {
        if (Yii::app()->user->isGuest) {
            $model=new UserLogin;
            // collect user input data
            if(isset($_POST['UserLogin']))
            {
                $model->attributes=$_POST['UserLogin'];
                // validate user input and redirect to previous page if valid
                if($model->validate()) {
                    $this->lastViset();
                    if (strpos(Yii::app()->user->returnUrl,'/index.php')!==false)
                        $this->redirect(Yii::app()->controller->module->returnUrl);
                    else
                        $this->redirect(Yii::app()->controller->module->returnUrl);
                }
            }
            // display the login form
            $this->render('/user/login',array('model'=>$model));
        } else
            $this->redirect(Yii::app()->controller->module->returnUrl);
    }

    private function lastViset() {
        $lastVisit = User::model()->notsafe()->findByPk(Yii::app()->user->id);
        $lastVisit->lastvisit = time();
        $lastVisit->save();
    }

but the sistem return this error

{
   "success": false,
   "message": "Event "UserLogin.onBeforeSave" is not defined.",
   "data":
   {
       "errorCode": 500,
       "message": "Event "UserLogin.onBeforeSave" is not defined."
   }
}

How can I do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant