Exemplo n.º 1
0
 public function authenticateRemoteUser($remoteUser)
 {
     $app = \App::getInstance();
     $authorized = f('auth.remoteAuthorize', $remoteUser);
     if ($authorized) {
         $users = \Norm\Norm::factory('User');
         $user = $users->findOne(array('username' => $remoteUser['username']));
         if (is_null($user)) {
             $user = $users->newInstance();
             $user['username'] = $remoteUser['username'];
             $user['first_name'] = $remoteUser['first_name'];
             $user['last_name'] = $remoteUser['last_name'];
             $user['birth_date'] = $remoteUser['birth_date'];
             $user['birth_place'] = $remoteUser['birth_place'];
         }
         if (!empty($remoteUser['normalized_username'])) {
             $user['normalized_username'] = $remoteUser['normalized_username'];
         }
         $user['email'] = $remoteUser['email'];
         $user['sso_account_id'] = $remoteUser['$id'];
         $user->save();
     } else {
         throw new \Exception('You are unauthorized to access this application. Please contact administrator.');
     }
     return $user->toArray();
 }
Exemplo n.º 2
0
 public function __construct($clazz, $config = NULL)
 {
     $this->app = \Bono\App::getInstance();
     $this->clazz = $clazz;
     $collection = \Norm\Norm::factory($clazz);
     $this->schema = $collection->schema();
     $globalConfig = $this->app->config('component.table');
     if (!isset($config['actions'])) {
         if (isset($globalConfig['mapping'][$clazz]['actions'])) {
             $config['actions'] = $globalConfig['mapping'][$clazz]['actions'];
         } elseif (isset($globalConfig['default']['actions'])) {
             $config['actions'] = $globalConfig['default']['actions'];
         } else {
             $config['actions'] = array('read' => NULL, 'update' => NULL, 'delete' => NULL);
         }
     }
     if (!isset($config['columns'])) {
         if (isset($globalConfig['mapping'][$clazz]['columns'])) {
             $config['columns'] = $globalConfig['mapping'][$clazz]['columns'];
         } elseif (isset($globalConfig['default']['columns'])) {
             $config['columns'] = $globalConfig['default']['columns'];
         }
     }
     if (empty($this->schema) && empty($config['columns'])) {
         throw new \Exception('Plain table needs collection schema or "component.table" configuration!');
     }
     $this->config = $config;
     $this->view = new \Slim\View();
     $this->view->setTemplatesDirectory(realpath(dirname(__FILE__) . '/../../../templates'));
     $this->view->set('self', $this);
 }
Exemplo n.º 3
0
 public function removed($model)
 {
     $histCollection = Norm::factory($model->getClass() . 'History');
     $history = $histCollection->newInstance();
     $history['model_id'] = $model['$id'];
     $history['type'] = 'remove';
     $history->save();
 }
 public function read($slug)
 {
     $entry = Norm::factory('Git')->findOne(array('slug' => $slug));
     if (is_null($entry)) {
         return $this->app->notFound();
     }
     $this->data['entry'] = $entry;
     $this->response->template('git/read');
 }
Exemplo n.º 5
0
 public function __construct($clazz, $config = NULL)
 {
     $this->app = \Bono\App::getInstance();
     $this->clazz = $clazz;
     $collection = \Norm\Norm::factory($clazz);
     $this->schema = $collection->schema();
     $this->globalConfig = $this->app->config('component.form');
     $this->config = isset($this->globalConfig['mapping'][$this->clazz]) ? $this->globalConfig['mapping'][$this->clazz] : NULL;
 }
 protected function createSlug($repo)
 {
     $counter = (int) Norm::factory('Git')->find(array('repo', $repo))->count();
     $repoName = preg_replace("/[^\\da-z]/i", '-', $repo);
     $slug = strtolower($repoName);
     if ($counter > 0) {
         $counter++;
         $slug = strtolower($repoName) . '-' . (string) $counter;
     }
     return $slug;
 }
Exemplo n.º 7
0
 public function initialize()
 {
     $app = $this->app;
     $app->get('/', function () use($app) {
         $query = $app->request->get('q') ?: '';
         $gits = Norm::factory('Git')->find()->sort(array('$updated_time' => -1))->match($query)->limit(10);
         $count = Norm::factory('Git')->find()->count();
         $app->response->set('gits', $gits);
         $app->response->set('query', $query);
         $app->response->set('count', $count);
         $app->response->template('home');
         $app->view->setLayout('layout');
     });
 }
Exemplo n.º 8
0
 public function formatInput($value, $entry = null)
 {
     if ($format = $this['inputFormat'] and is_callable($format)) {
         return $format($value, $entry, $this);
     }
     $foreign = Norm::factory($this['foreign']);
     if ($this['readonly']) {
         $criteria = array('group' => $this->get('foreignGroup'), $this->get('foreignKey') => $value);
         $entry = $foreign->findOne($criteria);
         $label = is_null($entry) ? null : $entry->get($this->get('foreignLabel'));
         return '<span class="field">' . $label . '</span>';
     }
     $entries = $foreign->find(array('group' => $this->get('foreignGroup')));
     return App::getInstance()->theme->partial('_schema/sysparam', array('self' => $this, 'value' => $value, 'entries' => $entries));
 }
Exemplo n.º 9
0
 public function authenticate(array $options = array())
 {
     $app = \App::getInstance();
     if (!isset($options['username']) && !isset($options['password'])) {
         return null;
     }
     $userCollection = \Norm\Norm::factory(@$this->options['userCollection'] ?: 'User');
     $user = $userCollection->findOne(array('!or' => array(array('username' => $options['username']), array('email' => $options['username']), array('normalized_username' => str_replace('.', '', $options['username'])))));
     if (function_exists('salt')) {
         $options['password'] = salt($options['password']);
     }
     if (is_null($user) || $user['password'] . '' !== $options['password']) {
         return null;
     }
     if (empty($options['keep'])) {
         $app->session->reset();
     } else {
         $app->session->reset(array('lifetime' => 365 * 24 * 60 * 60));
     }
     $_SESSION['user'] = $user->toArray();
     return $user->toArray();
 }
Exemplo n.º 10
0
 /**
  * Find reference of a foreign key.
  *
  * @param string $key
  * @param string $foreign
  * @param string $foreignLabel
  * @param string $foreignKey
  * @param int &$i
  *
  * @return string
  */
 public function getQueryReference($key = '', $foreign = '', $foreignLabel = '', $foreignKey = '', &$i)
 {
     $model = Norm::factory($foreign);
     $refSchemes = $model->schema();
     $foreignKey = $foreignKey ?: 'id';
     if ($foreignKey == '$id') {
         $foreignKey = 'id';
     }
     $query = $key . ' IN (SELECT ' . $foreignKey . ' FROM ' . strtolower($foreign) . ' WHERE ' . $foreignLabel . ' LIKE :f' . $i . ') ';
     $i++;
     return $query;
 }
Exemplo n.º 11
0
 public function formatPlain($value, $entry = null)
 {
     $value = $this->prepare($value);
     $label = '';
     if (is_null($value)) {
         return null;
     } elseif (is_array($this['foreign'])) {
         if (isset($this['foreign'][$value])) {
             $label = $this['foreign'][$value];
         }
     } elseif (is_callable($this['foreign'])) {
         $label = $this['foreign']($value);
     } else {
         $foreignEntry = Norm::factory($this['foreign'])->findOne(array($this['foreignKey'] => $value));
         if (is_string($this['foreignLabel'])) {
             $label = $foreignEntry[$this['foreignLabel']];
         } elseif (is_callable($this['foreignLabel'])) {
             $getLabel = $this['foreignLabel'];
             $label = $getLabel($foreignEntry);
         }
     }
     return $label;
 }
Exemplo n.º 12
0
 public function toJSON($value)
 {
     if (!is_string($this['foreign'])) {
         // FIXME should return translated value if non scalar foreign item
         return $value;
     }
     $foreignCollection = Norm::factory($this['foreign']);
     if (Norm::options('include')) {
         $foreignKey = $this['foreignKey'];
         $newValue = array();
         foreach ($value as $k => $v) {
             if (is_null($foreignKey)) {
                 $newValue[] = $foreignCollection->findOne($v);
             } else {
                 $newValue[] = $foreignCollection->findOne(array($this['foreignKey'] => $v));
             }
         }
         $value = $newValue;
     }
     return $value;
 }
Exemplo n.º 13
0
 public function filterUnique($key, $value, $data, $args = array())
 {
     if (is_null($value) || $value === '') {
         return '';
     }
     $clazz = $args[0];
     $field = isset($args[1]) ? $args[1] : $key;
     $model = \Norm\Norm::factory($clazz)->findOne(array($field => $value));
     if (isset($model) && $model['$id'] != $data['$id']) {
         throw FilterException::factory($key, 'Field %s must be unique')->args($this->rules[$key]['label']);
     }
     return $value;
 }
Exemplo n.º 14
0
 /**
  * Register a route model.
  *
  * @param string $key
  *
  * @return mixed
  */
 public function routeModel($key)
 {
     if (!isset($this->routeModels[$key])) {
         $Clazz = Inflector::classify($key);
         $collection = Norm::factory($this->schema($key)->get('foreign'));
         $this->routeModels[$key] = $collection->findOne($this->routeData($key));
     }
     return $this->routeModels[$key];
 }