Beispiel #1
0
 public function read()
 {
     $list = cfg('resources', 'models', $this->name);
     $rels = array('collection', 'model');
     $data = array();
     $pack = array();
     foreach ($list as $model) {
         $cname = \System\Loader::get_class_from_model($model);
         $model = \System\Loader::get_model_from_class($model);
         $schema = $cname::get_visible_schema($this->request->user);
         foreach ($schema['attrs'] as $attr) {
             if (in_array($attr['type'], $rels)) {
                 $rel_cname = \System\Loader::get_class_from_model($attr['model']);
                 $rel_model = \System\Loader::get_model_from_class($attr['model']);
                 if (!array_key_exists($rel_model, $pack)) {
                     $pack[$rel_model] = $rel_cname::get_visible_schema($this->request->user);
                 }
             }
         }
         $pack[$model] = $schema;
     }
     foreach ($pack as $name => $def) {
         $data[] = array("name" => $name, "parents" => array('model'), "static" => $def);
     }
     $this->content = json_encode(array("data" => $data));
 }
Beispiel #2
0
 public function request_decode()
 {
     $this->cname = \System\Loader::get_class_from_model($this->req('model'));
     $this->request_decode_pagination();
     $this->filters = $this->request_decode_part('filters');
     $this->sort = $this->request_decode_part('sort');
     $this->joins = $this->request_decode_part('join');
     if (!is_array($this->joins)) {
         $this->joins = array();
     }
 }
Beispiel #3
0
 public function run()
 {
     $rq = $this->request;
     $id = $this->req('id');
     $model = $this->req('model');
     $cname = \System\Loader::get_class_from_model($model);
     $response = array('message' => 'not-found', 'status' => 404);
     if (class_exists($cname) && is_subclass_of($cname, '\\System\\Model\\Perm')) {
         if ($item = $cname::find($id)) {
             if ($item->can_be($cname::DROP, $rq->user)) {
                 $item->drop();
                 $response['message'] = 'dropped';
                 $response['status'] = 200;
             } else {
                 $response['message'] = 'denied';
                 $response['status'] = 403;
             }
         }
     }
     $this->partial(null, $response);
 }
Beispiel #4
0
 public function run()
 {
     $rq = $this->request;
     $res = $this->response;
     $ren = $res->renderer;
     $userClass = \System\Loader::get_class_from_model(\System\Settings::get('godmode', 'userClass'));
     if ($rq->logged_in()) {
         $this->flow->redirect($ren->url('god_home'));
     } else {
         $f = $res->form(array("id" => 'core-user-login'));
         $f->input(array('type' => 'text', 'name' => 'login', 'label' => $ren->trans("gm-login-name"), 'required' => true));
         $f->input(array('type' => 'password', 'name' => 'password', 'label' => $ren->trans("gm-password"), 'required' => true));
         $f->submit($ren->trans('Log in'));
         if ($f->passed()) {
             $p = $f->get_data();
             if ($userClass::startSession($rq, $p['login'], $p['password'])) {
                 $this->flow->redirect($ren->url('god_home'));
             }
         }
         $f->out($this);
     }
 }
Beispiel #5
0
 public function run()
 {
     $rq = $this->request;
     $res = $this->response;
     $page = 0;
     $per_page = 1;
     $model = $this->req('model');
     $cname = \System\Loader::get_class_from_model($model);
     $exists = class_exists($cname) && is_subclass_of($cname, '\\System\\Model\\Perm');
     $send = array('status' => 404, 'message' => 'schema-not-found');
     if ($exists) {
         try {
             $schema = $cname::get_visible_schema($rq->user);
         } catch (\System\Error\AccessDenied $e) {
             $send['status'] = 403;
             $send['message'] = 'access-denied';
         }
         if ($schema) {
             $send['status'] = 200;
             $send['message'] = 'ok';
             $send['data'] = $schema;
         }
     }
     try {
         $debug = \System\Settings::get('dev', 'debug', 'backend');
     } catch (\System\Error $e) {
         $debug = true;
     }
     if (!$debug) {
         $max_age = \System\Settings::get('cache', 'resource', 'max-age');
         $res->header('Pragma', 'public,max-age=' . $max_age);
         $res->header('Cache-Control', 'public');
         $res->header('Expires', date(\DateTime::RFC1123, time() + $max_age + rand(0, 60)));
         $res->header('Age', '0');
     }
     $this->partial(null, $send);
 }
Beispiel #6
0
 public function run()
 {
     $id = $this->id;
     $new = $this->new;
     def($id);
     def($new, false);
     $model = $this->req('model');
     $rq = $this->request;
     $cname = \System\Loader::get_class_from_model($model);
     $response = array('message' => 'not-found', 'status' => 404);
     if (class_exists($cname) && is_subclass_of($cname, '\\System\\Model\\Perm')) {
         if ($item = $new ? new $cname() : $cname::find($id)) {
             $data = $rq->post();
             foreach ($data as $attr_name => $val) {
                 if ($item::has_attr($attr_name)) {
                     $def = $cname::get_attr($attr_name);
                     if (is_string($val)) {
                         if (preg_match('/^[\\{\\[].*[\\}\\]]$/', $val)) {
                             $val = \System\Json::decode(html_entity_decode($val));
                         }
                     }
                     if (in_array($def['type'], array('file', 'image'))) {
                         $helper_cname = '\\System\\File';
                         if ($def['type'] == 'image') {
                             $helper_cname = '\\System\\Image';
                         }
                         if (is_array($val)) {
                             if (any($val['method']) && any($val[$val['method']])) {
                                 $data = $rq->post($val[$val['method']]);
                                 if ($data) {
                                     $item->{$attr_name} = $helper_cname::from_tmp($data['tmp_name'], $data['name']);
                                 }
                             }
                         }
                     } else {
                         if ($def['type'] == 'password') {
                             $item->{$attr_name} = hash_passwd($val);
                         } else {
                             if ($def['type'] == 'bool') {
                                 if ($val == 'false') {
                                     $val = false;
                                 }
                                 $item->{$attr_name} = $val;
                             } else {
                                 if ($def['type'] == 'date') {
                                     $date = \DateTime::createFromFormat('Y-m-d', $val);
                                     if ($date) {
                                         $tz = new \DateTimeZone(\System\Settings::get('locales', 'timezone'));
                                         $date->setTimeZone($tz);
                                     } else {
                                         $date = null;
                                     }
                                     $item->{$attr_name} = $date;
                                 } else {
                                     if ($def['type'] == 'datetime') {
                                         $date = \DateTime::createFromFormat('Y-m-d\\TH:i:sO', $val);
                                         if ($date) {
                                             $tz = new \DateTimeZone(\System\Settings::get('locales', 'timezone'));
                                             $date->setTimeZone($tz);
                                         } else {
                                             $date = null;
                                         }
                                         $item->{$attr_name} = $date;
                                     } else {
                                         $item->{$attr_name} = $val;
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             $item->request = $rq;
             if ($item::has_attr('author') && $rq->user) {
                 $item->author = $rq->user;
             }
             try {
                 $item->save();
             } catch (\System\Error $e) {
                 $response['status'] = 500;
                 $response['message'] = $e->get_explanation();
             }
             if ($response['status'] != 500) {
                 $response['message'] = $new ? 'created' : 'saved';
                 $response['status'] = 200;
             }
             $response['data'] = $item->to_object();
         }
     }
     $this->partial(null, $response);
 }
Beispiel #7
0
 public static function from_path($path, array $opts)
 {
     $cname = \System\Loader::get_class_from_model('module.' . $path);
     return new $cname($opts);
 }
Beispiel #8
0
 public static function get_pattern_test($pat)
 {
     $attrs = self::get_pattern_attrs($pat);
     foreach ($attrs as $attr) {
         $cname = '\\System\\Router\\Arg\\' . \System\Loader::get_class_from_model($attr['type']);
         $test = '/\\{' . $attr['name'] . ':' . $attr['type'];
         if ($attr['required']) {
             $test .= '(:yes)?';
         } else {
             $test .= ':no';
         }
         if (isset($attr['choices'])) {
             $test .= ':' . implode(',', $attr['choices']);
             $sub = '(' . implode('|', $attr['choices']) . ')';
         } else {
             $sub = '(' . $cname::PATTERN . ')' . ($attr['required'] ? '' : '?');
         }
         $test .= '\\}/';
         $pat = preg_replace($test, $sub, $pat);
     }
     return $pat;
 }
Beispiel #9
0
 public static function get_visible_schema(\System\User $user)
 {
     if (static::can_user(static::VIEW_SCHEMA, $user)) {
         $cname = get_called_class();
         $schema = static::get_schema();
         $res = array();
         $rel_attrs = array('collection', 'model');
         foreach ($schema['attrs'] as $key => $attr) {
             if (in_array($attr['type'], $rel_attrs)) {
                 $rel_cname = \System\Loader::get_class_from_model($attr['model']);
                 if (class_exists($rel_cname) && is_subclass_of($rel_cname, '\\System\\Model\\Perm') && $rel_cname::can_user(static::VIEW_SCHEMA, $user)) {
                     $res[] = $attr;
                 }
             } else {
                 $res[] = $attr;
             }
         }
         $schema['attrs'] = $res;
         return $schema;
     } else {
         throw new \System\Error\AccessDenied();
     }
 }