public function __construct($arg = null) { if (is_array($arg)) { $this->schema = $arg; } else { $name = is_string($arg) ? $arg : f('controller.name'); $this->schema = \Norm::factory($name)->schema(); } $this->data = \App::getInstance()->request->post(); }
public function saving($model) { if ($model->isNew()) { $model['normalized_username'] = str_replace('.', '', $model['username']); $existingUser = \Norm::factory('User')->findOne(array('normalized_username' => $model['normalized_username'])); if ($existingUser) { throw new \Exception('Username apparently already exists'); } } }
public function call() { $app = $this->app; $app->get('/', function () use($app) { $q = $app->request->get('q'); if (empty($q)) { $entries = \Norm::factory('Workspace')->find(); } else { $entries = \Norm::factory('Workspace')->find(array('!or' => array(array('title!like' => $q), array('path!like' => $q), array('description!like' => $q)))); } $entries->sort(array('title' => 1))->limit(25); $app->response->set('entries', $entries); $app->response->set('q', $q); $app->response->template('xeekee/showcase'); }); $this->next->call(); }
public function call() { $options = array_merge(array('baseDir' => 'data'), $this->options ?: array()); $this->app = $app = \App::getInstance(); $this->request = $app->request; $this->response = $app->response; $app->get('/admin/workspace/:id/members', function ($id) use($app) { $entry = \Norm::factory('Workspace')->findOne($id); $app->response->set('entry', $entry); $app->response->template('admin/workspace/members'); }); $app->post('/admin/workspace/:id/members', function ($id) use($app) { try { $entry = \Norm::factory('Workspace')->findOne($id); $post = $app->request->post(); if (empty($post['members'])) { $members = array(); } else { foreach ($post['members'] as $member) { if (!empty($member)) { $members[] = $member; } } } $entry['members'] = $members; $entry->save(); h('notification.info', 'Member updated.'); } catch (\Exception $e) { h('notification.error', $e); } $app->response->set('entry', $entry); $app->response->template('admin/workspace/members'); }); $pathInfo = $app->request->getPathInfo(); if (!is_null($app->controller) || $pathInfo === '/logout' || $pathInfo === '/login' || $pathInfo === '/unauthorized') { $this->next->call(); return; } $app->container->singleton('xeekeeTopPage', function ($c) use($app) { return !(count($app->request->getSegments()) > 2); }); $app->get($pathInfo ?: '/', array($this, 'show')); $app->post($pathInfo ?: '/', array($this, 'edit')); $this->next->call(); }
public function call() { if ($this->app->config('bono.cli')) { return $this->next->call(); } $app = $this->app; $request = $app->request; $response = $app->response; $that = $this; $defaultOptions = array('unauthorizedUri' => '/unauthorized'); if (is_array($this->options)) { $this->options = array_merge($defaultOptions, $this->options); } else { $this->options = $defaultOptions; } if (isset($this->options['driver'])) { $Clazz = $this->options['driver']; } elseif (isset($this->options['class'])) { $Clazz = $this->options['class']; } else { throw new \Exception('No auth driver specified.'); } $app->auth = $driver = $this->driver = new $Clazz($this); if (!$driver instanceof \Xinix\BonoAuth\Driver\Auth) { throw new \Exception('Auth driver should be instance of Xinix\\BonoAuth\\Driver\\Auth.'); } // authentication needs SessionMiddleware if (!$app->has('Bono\\Middleware\\SessionMiddleware')) { throw new \Exception('Authentication need Bono\\Middleware\\SessionMiddleware.'); } // theme may get templates from bono-auth $f = explode('/src/', __FILE__); $f = $f[0]; $app->theme->addBaseDirectory($f); $app->filter('auth.html.link', function ($l) use($driver) { if ($driver->authorize($l['uri'])) { return '<a href="' . \URL::site($l['uri']) . '">' . $l['label'] . '</a>'; } }); $app->filter('auth.allowed', function ($l) use($driver) { return $driver->authorize($l); }); // TODO revisit notification.error write should be trapped from // notificationmiddleware automatically $app->get('/unauthorized', function () use($app, $response, $driver) { if (!empty($_GET['error'])) { h('notification.error', new AuthException($_GET['error'])); } else { h('notification.error', 'Unauthorized!'); } // $app->flashNow('error', '<p>Unauthorized!</p>'); $response->setStatus(401); $response->template('unauthorized'); }); $app->get('/login', function () use($app, $response, $driver) { $response->template('login'); try { $loginUser = $driver->authenticate(); if ($loginUser) { $driver->redirectBack(); } } catch (\Slim\Exception\Stop $e) { throw $e; } catch (\Exception $e) { h('notification.error', $e); // $app->flashNow('error', ''.$e); } }); $app->post('/login', function () use($app, $driver) { $app->response->template('login'); $entry = $body = $app->request->getBody(); try { $loginUser = $driver->authenticate($body); if (is_null($loginUser)) { throw new \Exception('Username or password not match'); } if (f('auth.login.success', $loginUser)) { $driver->redirectBack(); } $entry = $loginUser; } catch (\Slim\Exception\Stop $e) { throw $e; } catch (\Exception $e) { $app->response->setStatus(401); h('notification.error', $e); } $app->response->data('entry', $entry); }); $app->get('/logout', function () use($app, $driver) { h('notification.info', 'Good bye.'); $driver->revoke(); }); $app->get('/passwd', function () use($app) { $app->response->template('passwd'); }); $app->post('/passwd', function () use($app) { Filter::register('checkPassword', function ($value, $data) { if ($_SESSION['user']['password'] . '' === $value) { return $value; } else { throw new \Exception('Old password not valid'); } }); $filter = Filter::create(array('old' => 'trim|required|salt|checkPassword', 'new' => 'trim|required|confirmed|salt')); $app->response->template('passwd'); $data = $app->request->post(); try { $data = $filter->run($data); $user = \Norm::factory('User')->findOne($_SESSION['user']['$id']); $user['password'] = $data['new_confirmation']; $user['password_confirmation'] = $data['new_confirmation']; $user->save(); $_SESSION['user'] = $user->toArray(); if (f('auth.passwd.success', $user)) { h('notification.info', 'Your password is changed.'); } } catch (\Slim\Exception\Stop $e) { // noop } catch (\Exception $e) { h('notification.error', $e); } $app->response->data('entry', $data); }); $app->filter('auth.isAuthorized', function ($options) use($app) { return $app->auth->authorize($options); }); $app->filter('auth.authorize', function ($options) use($app) { if (is_array($options) && isset($options['uri'])) { $uri = $options['uri']; } else { $uri = $options; } switch ($uri) { case '/login': case '/logout': case '/unauthorized': return true; } return $options; }, 0); if ($driver->authorize($app->request->getResourceUri())) { return $this->next->call(); } else { $response->setStatus(401); $response->template('unauthorized'); // $response->redirect(\URL::create($this->options['unauthorizedUri'], array( // '!continue' => $driver->getRedirectUri(), // ))); } }
public function getWorkspace() { if ($this->workspace === '') { $this->workspace = \Norm::factory('Workspace')->findOne(array('path' => $this->pathInfo)); } return $this->workspace; }
@extends('layout') @section('content') <?php $schema = Norm::factory(f('controller.name'))->schema(); ?> <div class="row container"> <div class="top30"> <div class="twelve columns"> <form method="POST"> <ul class="breadcrumbs"> <li><a href="{{ URL::base() }}">Home</a></li> <li><a href="{{ f('controller.url') }}">{{ f('controller')->getClass() }}</a></li> <li class="current"><a href="{{ URL::current() }}">Update</a></li> </ul> <fieldset> <legend>{{ f('controller.name') }}</legend> @foreach ($schema as $name => $field) <div class="row"> <div class="three columns"> {{ $field->label() }} </div> <div class="nine columns"> {{ $field->formatInput($entry[$name], $entry) }} </div> </div> @endforeach <div class="row top30"> <div class="right"> <a href="{{ f('controller.url', '/'.$entry['$id']) }}" class="button radius secondary">Cancel</a>
<?php use Bono\Helper\URL; // $crit = array(); // if ($criteria && $entry) { // foreach ($criteria as $key => $v) { // $crit[$key] = @$entry[$v]; // } // } // $entries = Norm::factory($self['foreign'])->find($crit); $foreign = Norm::factory($self['foreign']); $controllerConfig = App::getInstance()->config('bono.controllers'); $lowerForeign = '/' . strtolower($self['foreign']); if (!array_key_exists($lowerForeign, $controllerConfig['mapping'])) { foreach ($controllerConfig['mapping'] as $k => $v) { if (strpos($k, $lowerForeign) !== FALSE) { $lowerForeign = $k; break; } } } ?> <input is="x-select" name="<?php echo $self['name']; ?> " value="<?php echo @$value; ?> " placeholder="<?php