public static function authenticate() { $security_token = self::get_security_token(); $security_token_key = moojon_config::get('security_token_key'); $primary_key = moojon_primary_key::NAME; $security_identity_key = moojon_config::get('security_identity_key'); $security_password_key = moojon_config::get('security_password_key'); $security_model_class = moojon_config::get('security_model'); $security_model = new $security_model_class(); $security_identity_data_type = $security_model->get_column($security_identity_key)->get_data_type(); $security_password_data_type = $security_model->get_column($security_password_key)->get_data_type(); $security_key = moojon_config::get('security_key'); $login_attempt = self::login_attempt($security_key); if (!$security_token) { if ($login_attempt) { $security = moojon_post::get($security_key); $security_identity_value = $security[$security_identity_key]; $security_password_value = $security[$security_password_key]; $where = "`{$security_identity_key}` = :{$security_identity_key} AND `{$security_password_key}` = CONCAT(salt, SHA1(CONCAT(salt, :{$security_password_key})))"; $param_values = array(":{$security_identity_key}" => $security_identity_value, ":{$security_password_key}" => $security_password_value); $param_data_types = array(":{$security_identity_key}" => $security_identity_data_type, ":{$security_password_key}" => $security_password_data_type); } else { return false; } } else { $where = "{$primary_key} = :{$primary_key}"; $param_values = array(":{$primary_key}" => $security_token); $column = new moojon_primary_key(); $param_data_types = array(":{$primary_key}" => $column->get_data_type()); } $records = $security_model->read($where, null, null, $param_values, $param_data_types); if (!$records->count) { self::destroy(); return false; } else { $security_remember_key = moojon_config::get('security_remember_key'); $security_token = $records->first->{$primary_key}; if ($login_attempt) { $security = moojon_post::get($security_key); if (array_key_exists($security_remember_key, $security)) { if (strlen($security[$security_remember_key])) { moojon_cookie::set($security_token_key, $security_token); } } } self::create_security_token($security_token); return $records->first; } }
public static function get_relationship_object_where(moojon_base_relationship $relationship, moojon_base_model $accessor) { $key = $relationship->get_key(); switch (get_class($relationship)) { case 'moojon_has_one_relationship': $foreign_table = $relationship->get_foreign_table(); $foreign_key = $relationship->get_foreign_key(); $return = "`{$foreign_table}`.`{$key}` = :{$foreign_key}"; break; case 'moojon_has_many_relationship': $foreign_table = $relationship->get_foreign_table(); $foreign_key = moojon_primary_key::get_foreign_key(get_class($accessor)); $return = "`{$foreign_table}`.`{$foreign_key}` = :{$key}"; break; case 'moojon_has_many_to_many_relationship': $foreign_table = moojon_inflect::pluralize($relationship->get_class($accessor)); $foreign_key1 = moojon_primary_key::get_foreign_key($relationship->get_foreign_table()); $foreign_key2 = moojon_primary_key::get_foreign_key(get_class($accessor)); $return = "`{$key}` IN (SELECT `{$foreign_key1}` FROM `{$foreign_table}` WHERE `{$foreign_key2}` = :{$key})"; break; case 'moojon_belongs_to_relationship': $foreign_key = moojon_primary_key::get_foreign_key(get_class($accessor)); $return = "`{$key}` = :{$foreign_key}"; break; } return $return; }
function relationship_tables(moojon_base_model $model, $relationships = array(), $column_names = array(), $attributes = array(), $no_records_messages = array(), $count = null) { if ($model->has_relationships()) { $relationships = $relationships ? $relationships : $model->get_relationships(); foreach ($relationships as $key => $value) { if (is_subclass_of($value, 'moojon_base_relationship')) { $relationships[$key] = $model->{$key}; } } $div = div_tag(); foreach ($relationships as $key => $value) { switch ($model->get_relationship_type($key)) { case 'moojon_has_many_relationship': case 'moojon_has_many_to_many_relationship': $div->add_child(h3_tag(title_text($key))); $relationship_model = $model->get_relationship_model($key); $div->add_child(actions_ul(array(new_member_tag($relationship_model)))); $div->add_child('<br /><br /><br />'); $child_column_names = array_key_exists($key, $column_names) ? $column_names[$key] : $relationship_model->get_ui_column_names(array(moojon_primary_key::get_foreign_key($model->get_table()))); $child_attributes = array_key_exists($key, $attributes) ? $attributes[$key] : array(); $child_no_records_message = array_key_exists($key, $no_records_messages) ? $no_records_messages[$key] : array(); $div->add_child(table_for($value, $child_column_names, $child_attributes, $child_no_records_message, $count)); break; } } return $div; } else { return null; } }
protected final function belongs_to($name = null, $foreign_table = null, $foreign_key = null, $key = null, $shared_columns = array()) { $name = $name ? $name : $this->class; $foreign_table = $this->table; $foreign_key = $foreign_key ? $foreign_key : moojon_primary_key::get_foreign_key($foreign_table); $key = $key ? $key : moojon_primary_key::NAME; if ($this->has_property($name)) { throw new moojon_exception("Duplicate property when adding relationship ({$name})"); } if (!$this->has_column($key)) { throw new moojon_exception("No such column to use as key for relationship ({$key})"); } $relationship = new moojon_belongs_to_relationship($name, $foreign_table, $foreign_key, $key, $this->get_column($key)); $relationship->set_shared_columns($shared_columns); $this->relationships[$name] = $relationship; }
function model_from_symbol($symbol) { $symbol_name = moojon_base::get_symbol_name($symbol); $class = moojon_primary_key::get_class($symbol_name); $id = moojon_primary_key::get_id_from_foreign_key($symbol); $model = new $class(); $method_name = "read_by_{$id}"; return $model->{$method_name}(moojon_request::get($symbol_name)); }
function title_text($column_name) { return ucfirst(str_replace('_', ' ', moojon_primary_key::get_class($column_name))); }
private function map_member_routes($uris = array()) { $id = array_shift($uris); $uri = implode('/', $uris); $id_property = $this->id_property; $params = array_merge($this->params, array($id_property => $id)); $relationship_routes = $this->get_relationship_routes(); $params['relationship_routes'] = $relationship_routes; $pattern = ''; $routes = array_merge(array_key_exists('member_routes', $this->params) ? $this->params['member_routes'] : array(), $relationship_routes); if ($routes && ($route = moojon_routes::map($uri, $routes, false))) { $new_params = $route->get_params(); if (array_key_exists('resource', $new_params)) { $id_property = moojon_primary_key::get_foreign_key($this->resource); $params[$id_property] = $id; } $pattern = ":{$id_property}/" . $route->get_pattern(); $params = array_merge($params, $new_params); } else { switch ($this->method) { case 'get': if (!$uri) { $pattern = ":{$id_property}/"; $params['action'] = 'show'; } else { if ($uri == 'edit' || $uri == 'delete' || $uri == 'show') { $pattern = ":{$id_property}/{$uri}"; $params['action'] = $uri; } } break; case 'post': $pattern = 'create'; $params['action'] = $pattern; break; case 'put': $pattern = 'update'; $params['action'] = $pattern; break; case 'delete': $pattern = 'destroy'; $params['action'] = $pattern; break; } } return $this->match_route($pattern, $params, false); }
public function add(moojon_base_model $model) { $key = $this->relationship->get_key(); $foreign_key = $this->relationship->get_foreign_key(); $accessor = $this->accessor; switch (get_class($this->relationship)) { case 'moojon_has_many_relationship': $model->{$foreign_key} = $accessor->{$key}; break; case 'moojon_has_many_to_many_relationship': if ($model->new_record) { $model->save(); } $class_name = $this->relationship->get_class($accessor); $class = new $class_name(); $key_property = moojon_primary_key::get_foreign_key($accessor->get_class()); $class->{$foreign_key} = $model->{$key}; $class->{$key_property} = $accessor->{$key}; $model = $class; break; } $this[] = $model; }