예제 #1
0
 public function assignRoles($id)
 {
     if (count($_POST) > 0) {
         $roles = Model::load('system.user_roles')->getJustWithUserId($id);
         foreach ($roles as $role) {
             $role->delete();
         }
         foreach ($_POST as $roleId) {
             $role = Model::load('system.user_roles')->getNew();
             $role->user_id = $id;
             $role->role_id = $roleId;
             $role->save();
         }
         Ntentan::redirect($this->route);
     }
     $item = $this->model->getJustFirstWithId($id);
     $roles = Model::load('system.roles')->getAll();
     $assignedRoles = Model::load('system.user_roles')->getJustWithUserId($id, array('fields' => array('role_id')))->toArray();
     $structuredAssignedRoles = array();
     foreach ($assignedRoles as $assignedRole) {
         $structuredAssignedRoles[$assignedRole['role_id']] = true;
     }
     $this->set('roles', $roles);
     $this->set('assigned_roles', $structuredAssignedRoles);
     $this->set('item', (string) $item);
 }
예제 #2
0
 /**
  * Sets up the fixture, for example, opens a network connection.
  * This method is called before a test is executed.
  */
 protected function setUp()
 {
     parent::setUp();
     \ntentan\caching\Cache::reset();
     $this->users = \ntentan\models\Model::load('users');
     $this->roles = \ntentan\models\Model::load('roles');
     $this->departments = \ntentan\models\Model::load('departments');
 }
예제 #3
0
파일: RolesBase.php 프로젝트: ntentan/wyf
 public function getPermission($permission)
 {
     $permission = Model::load('system.permissions')->getFirst(array('conditions' => array('role_id' => $this->id, 'permission' => $permission)));
     if ($permission->count() == 0) {
         return false;
     } else {
         return $permission->access;
     }
 }
예제 #4
0
 public function setPermissions()
 {
     $arguments = func_get_args();
     $id = array_shift($arguments);
     $role = $this->model->getFirstWithId($id);
     if (count($_POST) > 0) {
         foreach ($_POST as $permissionName => $path) {
             $permission = Model::load('system.permissions')->getFirst(array('conditions' => array('role_id' => $id, 'permission' => $permissionName)));
             if ($permission->count() == 0 && $path != 'no') {
                 $permission->setData(array('role_id' => $id, 'permission' => $permissionName, 'path' => $path, 'access' => true));
                 $permission->save();
             } else {
                 if ($path == 'no') {
                     $permission->access = false;
                     $permission->update();
                 } else {
                     $permission->access = true;
                     $permission->update();
                 }
             }
         }
         $role->menu_tree = json_encode($this->getMenuTree($role));
         $role->update();
     }
     $permissionItems = array();
     $baseRoute = implode('/', $arguments) . (count($arguments) > 0 ? '/' : '');
     $baseDirectory = Ntentan::$namespace . "/modules/{$baseRoute}";
     $dir = dir($baseDirectory);
     while (false !== ($entry = $dir->read())) {
         if ($entry == '.' || $entry == '..') {
             continue;
         }
         $path = getcwd() . "/{$baseDirectory}{$entry}";
         $class = Ntentan::camelize($entry) . 'Controller';
         if (file_exists("{$path}/{$class}.php")) {
             $controller = Controller::load("{$baseRoute}{$entry}", true);
             if (is_a($controller, "\\ntentan\\plugins\\wyf\\lib\\WyfController")) {
                 $permissionItem = array('type' => 'permission', 'label' => Ntentan::toSentence($entry), 'permissions' => array(), 'path' => "{$baseRoute}{$entry}");
                 foreach ($controller->getPermissions() as $permission => $description) {
                     $active = $role->getPermission($permission);
                     $permissionItem['permissions'][] = array('name' => $permission, 'description' => $description, 'active' => $active);
                 }
                 $permissionItems[] = $permissionItem;
             }
             continue;
         }
         $class = Ntentan::camelize($entry);
         if (file_exists("{$path}/{$class}.php")) {
             continue;
         }
         if (is_dir($path)) {
             $permissionItems[] = array('type' => 'link', 'label' => Ntentan::toSentence($entry), 'link' => Ntentan::getUrl("{$this->route}/set_permissions/{$id}/{$entry}"));
         }
     }
     $this->set('permission_items', $permissionItems);
     $this->set('role', (string) $role);
 }
예제 #5
0
 public function __construct($model)
 {
     $this->renderWithType = 'select';
     $model = \ntentan\models\Model::load($model);
     $entity = Ntentan::singular($model->getName());
     $this->setLabel(Ntentan::toSentence($entity));
     $this->setName("{$entity}_id");
     $options = $model->getAll();
     foreach ($options as $option) {
         $this->option((string) $option, $option->id);
     }
 }
예제 #6
0
 public function __construct($label, $model, $value = null, $extraConditions = array())
 {
     parent::__construct();
     $this->label = $label;
     $modelInstance = Model::load($model);
     if ($value === null) {
         $data = $modelInstance->get('all', count($extraConditions) > 0 ? array('conditions' => $extraConditions) : null);
     } else {
         $data = $modelInstance->get('all', array('fields' => array('id', $value), 'conditions' => count($extraConditions) > 0 ? $extraConditions : null));
     }
     $this->setName(Ntentan::singular($model) . "_id");
     for ($i = 0; $i < $data->count(); $i++) {
         $this->addOption($value == null ? $data[$i] : $data[$i][$value], $data[$i]["id"]);
     }
 }
예제 #7
0
 public function authLocalPassword($username, $password)
 {
     $usersModelClass = Model::getClassName($this->usersModel);
     $users = new $usersModelClass();
     $result = $users->getJustFirstWithUsername($username);
     if ($result->password == md5($password) && $result->blocked != '1') {
         $_SESSION["logged_in"] = true;
         $_SESSION["username"] = $username;
         $_SESSION["user_id"] = $result["id"];
         $_SESSION["user"] = $result->toArray();
         return true;
     } else {
         $this->message = "Invalid username or password!";
         return false;
     }
 }
예제 #8
0
 public function suggest($modelName)
 {
     $this->view->template = false;
     $this->view->layout = false;
     $model = Model::load($modelName);
     $conditions = array();
     $fields = array();
     foreach (explode("/", $_GET['search_fields']) as $searchField) {
         $conditions[] = sprintf("LOWER(%s) LIKE '%s%%'", $model->dataStore->escape($searchField), $model->dataStore->escape(strtolower($_GET['s'])));
     }
     foreach (explode("/", $_GET['fields']) as $field) {
         $fields[] = $model->dataStore->escape($field);
     }
     $response = $model->dataStore->query(sprintf("SELECT %s FROM %s WHERE %s LIMIT 10", implode(",", $fields), $model->dataStore->table, implode(" OR ", $conditions)));
     echo json_encode($response);
 }
예제 #9
0
파일: RelateField.php 프로젝트: ntentan/wyf
 public function __construct($model, $submodel)
 {
     $this->renderWithType = 'select';
     $model = \ntentan\models\Model::load($model);
     $submodel = \ntentan\models\Model::load($submodel);
     $entity = Ntentan::singular($submodel->getName());
     $parentEntity = Ntentan::singular($model->getName());
     $this->setLabel(Ntentan::toSentence($entity));
     $this->setName("{$entity}_id");
     $parentId = "{$parentEntity}_id";
     $options = $model->getAll();
     foreach ($options as $option) {
         $suboptions = $submodel->getAll(array('conditions' => array($parentId => $option->id)));
         foreach ($suboptions as $suboption) {
             $this->option("{$option} / {$suboption}", $suboption->id);
         }
     }
 }
예제 #10
0
 public function open($sessionName, $sessionId)
 {
     $this->session = Model::load('sessions');
 }
예제 #11
0
파일: Model.php 프로젝트: ekowabaka/ntentan
 /**
  * Loads a model.
  * @param string $model
  * @return Model
  */
 public static function load($modelRoute)
 {
     if ($modelRoute == '') {
         throw new ModelNotFoundException('Model route is empty');
     }
     $className = Model::getClassName($modelRoute);
     return new $className();
 }
예제 #12
0
 public function console()
 {
     $this->setupConsoleView();
     $this->view->template = 'console.tpl.php';
     $arguments = func_get_args();
     if (count($arguments) == 0) {
         // Do nothing
     } else {
         if (end($arguments) == "add") {
             array_pop($arguments);
             $sectionkey = implode(".", $arguments);
             $this->model = Model::load($this->sections[$sectionkey]['model']);
             $this->entity = $this->sections[$sectionkey]['entity'];
             $this->consoleModeRoute = "{$this->prefix}{$this->controller->route}/console/{$this->sections[$sectionkey]['route']}";
             $this->add();
         } else {
             if (is_numeric(end($arguments))) {
                 $index = array_pop($arguments);
                 $action = array_pop($arguments);
                 if (end($arguments) == "confirm") {
                     array_pop($arguments);
                     $sectionkey = implode(".", $arguments);
                     $this->model = Model::load($this->sections[$sectionkey]['model']);
                     $this->entity = $this->sections[$sectionkey]['entity'];
                     $this->consoleModeRoute = "{$this->prefix}{$this->controller->route}/console/{$this->sections[$sectionkey]['route']}";
                     $this->confirm($action, $index);
                 } else {
                     $sectionkey = implode(".", $arguments);
                     $this->model = Model::load($this->sections[$sectionkey]['model']);
                     $this->entity = $this->sections[$sectionkey]['entity'];
                     $this->consoleModeRoute = "{$this->prefix}{$this->controller->route}/console/{$this->sections[$sectionkey]['route']}";
                     switch ($action) {
                         case "edit":
                             $this->edit($index);
                             break;
                         case "delete":
                             $this->delete($index);
                             break;
                         case 'page':
                             $this->showConsolePage($index);
                             break;
                         default:
                             $extensionMethodName = Ntentan::camelize(Ntentan::plural($this->entity), ".", "", true) . 'Admin' . Ntentan::camelize($action);
                             if (method_exists($this->controller, $extensionMethodName)) {
                                 $this->view->template = "{$this->entity}_{$action}.tpl.php";
                                 $extensionMethod = new ReflectionMethod($this->controller, $extensionMethodName);
                                 $extensionMethod->invoke($this->controller, $index);
                             } else {
                                 throw new MethodNotFoundException("Could not find {$extensionMethodName} method in the admin controller");
                             }
                             break;
                     }
                 }
             } else {
                 $sectionkey = implode(".", $arguments);
                 $this->model = Model::load($this->sections[$sectionkey]['model']);
                 $this->entity = $this->sections[$sectionkey]['entity'];
                 $this->consoleModeRoute = "{$this->prefix}{$this->controller->route}/console/{$this->sections[$sectionkey]['route']}";
                 $this->showConsolePage(1);
             }
         }
     }
 }
예제 #13
0
 public function __get($property)
 {
     switch ($property) {
         case "view":
             $viewInstance = $this->getViewInstance();
             if ($viewInstance == null) {
                 $viewInstance = new View();
                 $this->setViewInstance($viewInstance);
                 $viewInstance->defaultTemplatePath = $this->filePath;
             }
             return $viewInstance;
         case "layout":
             return $this->view->layout;
         case "model":
             if ($this->modelInstance == null) {
                 $this->modelInstance = Model::load($this->modelRoute);
             }
             return $this->modelInstance;
         case "directory":
             return Ntentan::$modulesPath . $this->route . "/";
         default:
             if (substr($property, -9) == "Component") {
                 $component = substr($property, 0, strlen($property) - 9);
                 return $this->getComponentInstance($component);
             } else {
                 throw new \Exception("Unknown property *{$property}* requested");
             }
     }
 }
예제 #14
0
 public function rest()
 {
     $this->view->layout = false;
     $this->view->template = false;
     $params = func_get_args();
     if (is_numeric(end($params))) {
         $id = array_pop($params);
     }
     $format = null;
     //Determine the data format
     $lastItem = explode('.', end($params));
     if (count($lastItem) == 2) {
         $format = end($lastItem);
         $lastItem = reset($lastItem);
         array_pop($params);
         array_push($params, $lastItem);
     }
     $modelName = implode('.', $params);
     try {
         $model = Model::load($modelName);
     } catch (ModelException $e) {
         $this->error("Failed to load model {$modelName}");
         die;
     }
     switch ($_SERVER['REQUEST_METHOD']) {
         case 'GET':
             if ($id != '') {
                 $response = $model->getFirstWithId($id);
             } else {
                 $response = $model->getAll();
             }
             print json_encode($response->toArray());
             break;
         case 'PUT':
             parse_str(file_get_contents("php://input"), $data);
             $validate = $model->setData($data);
             try {
                 $model->update($model->getKeyField(), $id);
                 http_response_code(201);
                 print json_encode($id);
             } catch (ModelException $e) {
                 http_response_code(400);
                 $this->error($e->getMessage());
             } catch (Exception $e) {
                 http_response_code(400);
                 $this->error($e->getMessage());
             }
             break;
         case 'POST':
             if ($format == 'json') {
                 $data = json_decode(file_get_contents("php://input"), true);
             } else {
                 $data = $_POST;
             }
             $model->setData($data);
             $id = $model->save();
             if ($id === false) {
                 http_response_code(400);
                 print json_encode($model->invalidFields);
             } else {
                 print json_encode($id);
             }
             break;
     }
 }
예제 #15
0
 protected function resolveName($fieldPath, $reformat = false, $description = null, $alias = true)
 {
     if ($reformat === true) {
         if (strpos($fieldPath, ".") === false) {
             if ($description['fields'][$fieldPath]['type'] == 'boolean') {
                 $field = $this->quotedTable . "." . $this->quote($fieldPath);
                 return " CASE WHEN {$field} = true THEN 1 WHEN {$field} = false THEN 0 END " . ($alias ? "AS {$fieldPath}" : '');
             } else {
                 return $this->quotedTable . "." . $this->quote($fieldPath);
             }
         } else {
             $modelPathArray = explode(".", $fieldPath);
             $fieldName = array_pop($modelPathArray);
             $modelPath = implode(".", $modelPathArray);
             $model = Model::load($modelPath);
             $description = $model->describe();
             if ($description[$fieldPath] == 'boolean') {
                 $fieldPath = $this->quote($model->datasStore->table) . '.' . $this->quote($fieldName);
                 return "CASE WHEN {$fieldPath} = true THEN 1 WHEN {$fieldPath} = false THEN 0 END ";
             } else {
                 return $this->quote($model->dataStore->table) . '.' . $this->quote($fieldName);
             }
         }
     } else {
         return parent::resolveName($fieldPath);
     }
 }
예제 #16
0
 protected function _put($data)
 {
     $fields = array_keys($data);
     $subData = array();
     if ($fields[0] == "0") {
         $fields = array_keys($data[0]);
         $quotedFields = array();
         foreach ($quotedFields as $field) {
             $quotedFields[] = $this->quote($field);
         }
         $query = "INSERT INTO " . ($this->schema != '' ? $this->quotedSchema . "." : '') . "{$this->quotedTable} (" . implode(", ", $quotedFields) . ") VALUES ";
         $baseQueries = array();
         foreach ($data as $row) {
             $values = array();
             foreach ($row as $value) {
                 $values[] = $value === "" || $value === null ? "NULL" : "'" . $this->escape($value) . "'";
             }
             $baseQueries[] = "( " . implode(", ", $values) . " )";
         }
         $query .= implode(",", $baseQueries);
         $this->query($query);
         $id = true;
     } else {
         $dataFields = array();
         $quotedDataFields = array();
         foreach ($data as $field => $value) {
             if (is_array($value)) {
                 $subData[$field] = $value;
             } else {
                 $values[] = $value === "" || $value === null ? "NULL" : "'" . $this->escape($value) . "'";
                 $dataFields[] = $field;
                 $quotedDataFields[] = $this->quote($field);
             }
         }
         $query = "INSERT INTO " . ($this->schema != '' ? $this->quotedSchema . "." : '') . "{$this->quotedTable} (" . implode(", ", $quotedDataFields) . ") VALUES (" . implode(", ", $values) . ")";
         $this->query($query);
         if (array_search('id', $dataFields) === false) {
             $id = $this->getLastInsertId();
         } else {
             $id = $data['id'];
         }
         foreach ($subData as $modelName => $data) {
             $model = Model::load($modelName);
             $table = $model->dataStore->table;
             $fields = array_keys($data[0]);
             $fields[] = Ntentan::singular($this->model->name) . "_id";
             $query = "INSERT INTO {$table} (" . implode(", ", $fields) . ") VALUES ";
             $dataQueries = array();
             foreach ($data as $newEntry) {
                 $values = array();
                 foreach ($newEntry as $value) {
                     $values[] = $value = "" ? "NULL" : "'" . $this->escape($value) . "'";
                 }
                 $values[] = $id;
                 $dataQueries[] = "(" . implode(", ", $values) . ")";
             }
             $query .= implode(", ", $dataQueries);
             $this->query($query);
         }
     }
     return $id;
 }