phalconplus Documentation v1.2.0
Class
Class PhalconPlus Auth

# Model

        namespace PhalconPlus\Auth;
    use PhalconPlus\Contracts\Auth\Access\ModelEvent;
    use Phalcon\Mvc\Model as ARModel;
    use Phalcon\Events\Event;
    use PhalconPlus\Contracts\Auth\Policy as PolicyContract;
    use PhalconPlus\Contracts\Auth\UserProvider;

    class Model implements ModelEvent
    {
        protected user = null { get };
        protected policies = [
        ];

        public function __construct( user)
        {
            let this->user = user;
        }

        public function setUser( user) ->
        {
            let this->user = user;
            return this;
        }

        public function beforeDelete( event,  model, var context = null) -> bool
        {
            var instance = this->getPolicy(model);
            return instance ? instance->delete(this->user, model) : false;
        }

        public function beforeCreate( event,  model, var context = null) -> bool
        {
            var instance = this->getPolicy(model);
            return instance ? instance->create(this->user, model) : false;
        }

        public function beforeUpdate( event,  model, var context = null) -> bool
        {
            var instance = this->getPolicy(model);
            return instance ? instance->update(this->user, model) : false;
        }

        public function beforeValidation( event,  model, var context = null) -> bool
        {
            return true;
        }

        public function getPolicy(var model) ->  | null
        {
            var modelClass, policyClass, instance;
            if typeof model == "object" && model instanceof ARModel {
                let modelClass = get_class(model);
            }  else {
                let modelClass = (string) model;
            }
            if fetch policyClass, this->policies[modelClass] {
                let instance = new {policyClass}();
            } else {
                let instance = new Policy();
            }
            return instance;
        }

    }