Beispiel #1
0
 /**
  * 前処理、入力値のバリデーションやログイン処理を行う
  * __before__メソッドを定義することで拡張する
  */
 public function before()
 {
     list(, $method) = explode('::', $this->get_selected_pattern()['action']);
     $annon = \ebi\Annotation::get_method(get_class($this), $method, ['http_method', 'request', 'user_role']);
     if (isset($annon['http_method']['value']) && strtoupper($annon['http_method']['value']) != \ebi\Request::method()) {
         throw new \ebi\exception\BadMethodCallException('Method Not Allowed');
     }
     if (isset($annon['request'])) {
         foreach ($annon['request'] as $k => $an) {
             if (isset($an['type'])) {
                 try {
                     \ebi\Validator::type($k, $this->in_vars($k), $an);
                 } catch (\ebi\exception\InvalidArgumentException $e) {
                     \ebi\Exceptions::add($e, $k);
                 }
                 \ebi\Validator::value($k, $this->in_vars($k), $an);
             }
         }
     }
     \ebi\Exceptions::throw_over();
     if (method_exists($this, '__before__')) {
         $this->__before__();
     }
     if ($this->has_object_plugin('before_flow_action_request')) {
         /**
          * 前処理
          * @param \ebi\flow\Request $arg1
          */
         $this->call_object_plugin_funcs('before_flow_action_request', $this);
     }
     if (!$this->is_user_logged_in() && (isset($this->login_anon) || $this->has_object_plugin('login_condition'))) {
         $this->login_required();
     }
     if ($this->is_user_logged_in() && (isset($annon['user_role']) || isset($this->login_anon['user_role']))) {
         if (!in_array(\ebi\UserRole::class, \ebi\Util::get_class_traits(get_class($this->user()))) || isset($this->login_anon['user_role']) && !in_array($this->login_anon['user_role'], $this->user()->get_role()) || isset($annon['user_role']['value']) && !in_array($annon['user_role']['value'], $this->user()->get_role())) {
             throw new \ebi\exception\NotPermittedException();
         }
     }
 }
Beispiel #2
0
 /**
  * 値の妥当性チェックを行う
  */
 public function validate()
 {
     foreach ($this->columns(true) as $name => $column) {
         if (!\ebi\Exceptions::has($name)) {
             $value = $this->{$name}();
             \ebi\Validator::value($name, $value, ['type' => $this->prop_anon($name, 'type'), 'min' => $this->prop_anon($name, 'min'), 'max' => $this->prop_anon($name, 'max'), 'require' => $this->prop_anon($name, 'require')]);
             $unique_together = $this->prop_anon($name, 'unique_together');
             if ($value !== '' && $value !== null && ($this->prop_anon($name, 'unique') === true || !empty($unique_together))) {
                 $uvalue = $value;
                 $q = [\ebi\Q::eq($name, $uvalue)];
                 if (!empty($unique_together)) {
                     foreach (is_array($unique_together) ? $unique_together : [$unique_together] as $c) {
                         $q[] = Q::eq($c, $this->{$c}());
                     }
                 }
                 foreach ($this->primary_columns() as $primary) {
                     if (null !== $this->{$primary->name()}) {
                         $q[] = Q::neq($primary->name(), $this->{$primary->name()});
                     }
                 }
                 if (0 < call_user_func_array([get_class($this), 'find_count'], $q)) {
                     \ebi\Exceptions::add(new \ebi\exception\UniqueException($name . ' unique'), $name);
                 }
             }
             $master = $this->prop_anon($name, 'master');
             if (!empty($master)) {
                 $master = str_replace('.', "\\", $master);
                 if ($master[0] !== '\\') {
                     $master = '\\' . $master;
                 }
                 try {
                     $r = new \ReflectionClass($master);
                 } catch (\ReflectionException $e) {
                     $self = new \ReflectionClass(get_class($this));
                     $r = new \ReflectionClass("\\" . $self->getNamespaceName() . $master);
                 }
                 $mo = $r->newInstanceArgs();
                 $primarys = $mo->primary_columns();
                 if (empty($primarys) || 0 === call_user_func_array([$mo, 'find_count'], [Q::eq(key($primarys), $this->{$name})])) {
                     \ebi\Exceptions::add(new \ebi\exception\NotFoundException($name . ' master not found'), $name);
                 }
             }
             try {
                 if (method_exists($this, '__verify_' . $column->name() . '__') && call_user_func([$this, '__verify_' . $column->name() . '__']) === false) {
                     \ebi\Exceptions::add(new \ebi\exception\VerifyException($column->name() . ' verification failed'), $column->name());
                 }
             } catch (\ebi\Exceptions $e) {
             } catch (\Exception $e) {
                 \ebi\Exceptions::add($e, $column->name());
             }
         }
     }
     \ebi\Exceptions::throw_over();
 }
Beispiel #3
0
 private function ___set___($v)
 {
     if ($this->prop_anon($this->_, 'set') === false) {
         throw new \ebi\exception\InvalidArgumentException('not permitted');
     }
     $anon = $this->prop_anon($this->_);
     switch ($this->prop_anon($this->_, 'attr')) {
         case 'a':
             $v = func_num_args() > 1 ? func_get_args() : (is_array($v) ? $v : [$v]);
             foreach ($v as $a) {
                 $this->{$this->_}[] = \ebi\Validator::type($this->_, $a, $anon);
             }
             break;
         case 'h':
             $v = func_num_args() === 2 ? [func_get_arg(0) => func_get_arg(1)] : (is_array($v) ? $v : [(string) $v => $v]);
             foreach ($v as $k => $a) {
                 $this->{$this->_}[$k] = \ebi\Validator::type($this->_, $a, $anon);
             }
             break;
         default:
             $this->{$this->_} = \ebi\Validator::type($this->_, $v, $anon);
     }
     return $this;
 }