I am using Laravel Tastyigniter system in which I want to show the locations name in dropdown in Menus module according to locations added by admin which is currently logged in.
For Example, If admin A added two locations such as location A and location B and
admin B added two locations such as location C and location D resp.
Note - The locations are getting saved in database with created_by column which is id of admin adding the location.
A) What supposed to happen -
If I logged in as admin A then in location dropdown Location A and Location B should get display
If I logged in as admin B then in location dropdown Location C and Location D should get display.
B) What is happening currently -
For both the admins all the 4 locations are getting displayed.
C) Following is the Code -
Here is Menus_model.php
<?php namespace Admin\Models;
use Admin\Traits\Locationable;
use Igniter\Flame\Database\Traits\Purgeable;
class Menus_model extends Model
{
use Purgeable;
use Locationable;
const LOCATIONABLE_RELATION = 'locations';
public $relation = [
'morphToMany' => [
'locations' => ['Admin\Models\Locations_model', 'name' =>
'locationable'],
],
];
protected $purgeable = ['locations'];
}
Here is menus_model.php which is present under models->config
<?php
$config['form']['tabs'] = [
'fields' => [
'locations' => [
'label' => 'Location',
'type' => 'relation',
'span' => 'right',
'valueFrom' => 'locations',
'nameFrom' => 'location_name',
'locationAware' => 'hide',
],
],
];
return $config;
Here is the Locations_model.php file code under models folder
<?php namespace Admin\Models;
use Admin\Traits\HasDeliveryAreas;
use Admin\Traits\HasWorkingHours;
use Igniter\Flame\Database\Attach\HasMedia;
use Igniter\Flame\Database\Traits\HasPermalink;
use Igniter\Flame\Database\Traits\Purgeable;
use Igniter\Flame\Location\Models\AbstractLocation;
use DB;
/**
* Locations Model Class
*
* #package Admin
*/
class Locations_model extends AbstractLocation
{
use HasWorkingHours;
use HasDeliveryAreas;
use HasPermalink;
use Purgeable;
use HasMedia;
const LOCATION_CONTEXT_SINGLE = 'single';
const LOCATION_CONTEXT_MULTIPLE = 'multiple';
protected $appends = ['location_thumb'];
protected $hidden = ['options'];
public $casts = [
'location_country_id' => 'integer',
'location_lat' => 'double',
'location_lng' => 'double',
'offer_delivery' => 'boolean',
'offer_collection' => 'boolean',
'delivery_time' => 'integer',
'collection_time' => 'integer',
'last_order_time' => 'integer',
'reservation_time_interval' => 'integer',
'reservation_stay_time' => 'integer',
'location_status' => 'boolean',
'options' => 'serialize',
'location_city' => 'integer',
'region_id'=>'integer',
];
public $relation = [
'hasMany' => [
'working_hours' => ['Admin\Models\Working_hours_model', 'delete' =>
TRUE],
'delivery_areas' => ['Admin\Models\Location_areas_model', 'delete'
=> TRUE],
'reviews' => ['Admin\Models\Reviews_model', 'delete' => TRUE],
],
'belongsTo' => [
'country' => ['System\Models\Countries_model', 'otherKey' =>
'country_id', 'foreignKey' => 'location_country_id'],
'city' => ['Admin\Models\City_model', 'otherKey' => 'city_id', 'foreignKey' => 'location_city'],
'region' => ['Admin\Models\Region_model', 'otherKey' => 'region_id', 'foreignKey' => 'region_id'],
],
'belongsToMany' => [
'tables' => ['Admin\Models\Tables_model', 'table' => 'location_tables'],
'cuisines' => ['Admin\Models\Cuisines_model', 'table' => 'location_cuisines'],
],
];
protected $purgeable = ['tables', 'delivery_areas','cuisines'];
public $permalinkable = [
'permalink_slug' => [
'source' => 'location_name',
'controller' => 'local',
],
];
public $mediable = [
'thumb',
'gallery' => ['multiple' => TRUE],
];
protected static $allowedSortingColumns = [
'distance asc', 'distance desc',
'reviews_count asc', 'reviews_count desc',
'location_id asc', 'location_id desc',
'location_name asc', 'location_name desc',
];
public $url;
protected static $defaultLocation;
public static function onboardingIsComplete()
{
if (!$defaultId = params('default_location_id'))
return FALSE;
if (!$model = self::isEnabled()->find($defaultId))
return FALSE;
return isset($model->getAddress()['location_lat'])
AND isset($model->getAddress()['location_lng'])
AND ($model->hasDelivery() OR $model->hasCollection())
AND isset($model->options['hours'])
AND $model->delivery_areas->where('is_default', 1)->count() > 0;
}
public function getWeekDaysOptions()
{
return ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
}
//
// Events
//
protected function afterFetch()
{
$this->parseOptionsValue();
}
protected function beforeSave()
{
$this->parseOptionsValue();
}
protected function afterSave()
{
$this->performAfterSave();
}
protected function beforeDelete()
{
Location_tables_model::where('location_id', $this->getKey())->delete();
Location_cuisines_model::where('location_id', $this->getKey())->delete();
}
//
// Scopes
//
/**
* Scope a query to only include enabled location
*
* #return $this
*/
public function scopeIsEnabled($query)
{
return $query->where('location_status', 1);
}
public function scopeListFrontEnd($query, array $options = [])
{
extract(array_merge([
'page' => 1,
'pageLimit' => 20,
'sort' => null,
'search' => null,
'latitude' => null,
'longitude' => null,
], $options));
if ($latitude AND $longitude)
$query->selectDistance($latitude, $longitude);
$searchableFields = ['location_name', 'location_address_1', 'location_address_2', 'location_city',
'location_state', 'location_postcode', 'description'];
if (!is_array($sort)) {
$sort = [$sort];
}
foreach ($sort as $_sort) {
if (in_array($_sort, self::$allowedSortingColumns)) {
$parts = explode(' ', $_sort);
if (count($parts) < 2) {
array_push($parts, 'desc');
}
[$sortField, $sortDirection] = $parts;
$query->orderBy($sortField, $sortDirection);
}
}
$search = trim($search);
if (strlen($search)) {
$query->search($search, $searchableFields);
}
return $query->paginate($pageLimit, $page);
}
//
// Accessors & Mutators
//
public function getLocationThumbAttribute()
{
return $this->hasMedia() ? $this->getThumb() : null;
}
public function getDeliveryTimeAttribute($value)
{
return (int)$value;
}
public function getCollectionTimeAttribute($value)
{
return (int)$value;
}
public function getFutureOrdersAttribute($value)
{
return (bool)$value;
}
public function getReservationTimeIntervalAttribute($value)
{
return (int)$value;
}
//
// Helpers
//
public function setUrl($suffix = null)
{
if (is_single_location())
$suffix = '/menus';
$this->url = site_url($this->permalink_slug.$suffix);
}
public function hasGallery()
{
return $this->hasMedia('gallery');
}
public function getGallery()
{
$gallery = array_get($this->options, 'gallery');
$gallery['images'] = $this->getMedia('gallery');
return $gallery;
}
public function parseOptionsValue()
{
$value = #unserialize($this->attributes['options']) ?: [];
$this->parseHoursFromOptions($value);
$this->parseAreasFromOptions($value);
$this->attributes['options'] = #serialize($value);
return $value;
}
public function listAvailablePayments()
{
$result = [];
$payments = array_get($this->options, 'payments', []);
$paymentGateways = Payments_model::listPayments();
foreach ($paymentGateways as $payment) {
if ($payments AND !in_array($payment->code, $payments)) continue;
$result[$payment->code] = $payment;
}
return collect($result);
}
public function performAfterSave()
{
$this->restorePurgedValues();
if (array_key_exists('hours', $this->options)) {
$this->addOpeningHours($this->options['hours']);
}
if (array_key_exists('delivery_areas', $this->attributes)) {
$this->addLocationAreas($this->attributes['delivery_areas']);
}
if (array_key_exists('tables', $this->attributes)) {
$this->addLocationTables($this->attributes['tables']);
}
if (array_key_exists('cuisines', $this->attributes)) {
$this->addLocationCuisines($this->attributes['cuisines']);
}
}
public static function getDefault()
{
if (self::$defaultLocation !== null) {
return self::$defaultLocation;
}
$defaultLocation = self::isEnabled()->where('location_id', params('default_location_id'))->first();
if (!$defaultLocation) {
$defaultLocation = self::isEnabled()->first();
if ($defaultLocation) {
params('default_location_id', $defaultLocation->getKey());
params()->save();
}
}
return self::$defaultLocation = $defaultLocation;
}
/**
* Create a new or update existing location tables
*
* #param array $tables
*
* #return bool
*/
public function addLocationTables($tables = [])
{
return $this->tables()->sync($tables);
}
public function addLocationCuisines($cuisines = [])
{
return $this->cuisines()->sync($cuisines);
}
}
Here is locations_model.php which is present under models->config folder
<?php
$config['form']['tabs'] = [
'defaultTab' => 'lang:admin::lang.locations.text_tab_general',
'fields' => [
'location_name' => [
'label' => 'lang:admin::lang.label_name',
'type' => 'text',
'span' => 'left',
],
'location_email' => [
'label' => 'lang:admin::lang.label_email',
'type' => 'text',
'span' => 'right',
],
'location_telephone' => [
'label' => 'lang:admin::lang.locations.label_telephone',
'type' => 'text',
'span' => 'left',
],
'location_status' => [
'label' => 'lang:admin::lang.label_status',
'type' => 'switch',
'default' => 1,
'span' => 'right',
],
'created_by' => [
'type' => 'hidden',
'default' => isset($_SESSION['user_id']) ? $_SESSION['user_id'] : '',
],
],
];
return $config;
UPDATED
Basically I want to diaply locations in menus form , Currently in menus form all the locations are getting display and the code for this is mentioned below
This is Menus.php controller
<?php namespace Admin\Controllers;
use Admin\Classes\AdminController;
use Admin\Models\Menu_options_model;
use AdminMenu;
use ApplicationException;
class Menus extends AdminController
{
public $implement = [
'Admin\Actions\ListController',
'Admin\Actions\FormController',
'Admin\Actions\LocationAwareController',
];
public $listConfig = [
'list' => [
'model' => 'Admin\Models\Menus_model',
'title' => 'lang:admin::lang.menus.text_title',
'emptyMessage' => 'lang:admin::lang.menus.text_empty',
'defaultSort' => ['menu_id', 'DESC'],
'configFile' => 'menus_model',
],
];
protected $requiredPermissions = 'Admin.Menus';
public function __construct()
{
parent::__construct();
AdminMenu::setContext('menus');
}
public function edit_onChooseMenuOption($context, $recordId)
{
$menuOptionId = post('Menu._options');
if (!$menuOption = Menu_options_model::find($menuOptionId))
throw new ApplicationException('Please select a menu option to
attach');
$model = $this->asExtension('FormController')->formFindModelObject($recordId);
$menuItemOption = $model->menu_options()->create(['option_id' => $menuOptionId]);
$menuOption->option_values()->get()->each(function ($model) use ($menuItemOption) {
$menuItemOption->menu_option_values()->create([
'menu_option_id' => $menuItemOption->menu_option_id,
'option_value_id' => $model->option_value_id,
'new_price' => $model->price,
]);
});
$model->reload();
$this->asExtension('FormController')->initForm($model, $context);
flash()->success(sprintf(lang('admin::lang.alert_success'), 'Menu item option attached'))->now();
$formField = $this->widgets['form']->getField('menu_options');
return [
'#notification' => $this->makePartial('flash'),
'#'.$formField->getId('group') => $this->widgets['form']->renderField($formField, [
'useContainer' => FALSE,
]),
];
}
}
Below is Locaations.php controller
<?php namespace Admin\Controllers;
use Admin\Facades\AdminLocation;
use Admin\Models\Locations_model;
use AdminMenu;
use Exception;
use Geocoder;
class Locations extends \Admin\Classes\AdminController
{
public $implement = [
'Admin\Actions\ListController',
'Admin\Actions\FormController',
];
public $listConfig = [
'list' => [
'model' => 'Admin\Models\Locations_model',
'title' => 'lang:admin::lang.locations.text_title',
'emptyMessage' => 'lang:admin::lang.locations.text_empty',
'defaultSort' => ['location_id', 'DESC'],
'configFile' => 'locations_model',
],
];
protected $requiredPermissions = 'Admin.Locations';
public function __construct()
{
parent::__construct();
AdminMenu::setContext('locations', 'restaurant');
}
public function remap($action, $params)
{
if ($action != 'settings' AND AdminLocation::check())
return $this->redirect('locations/settings');
return parent::remap($action, $params);
}
public function settings($context = null)
{
if (!AdminLocation::check())
return $this->redirect('locations');
$this->asExtension('FormController')->edit('edit', $this-
>getLocationId());
}
public function index_onSetDefault($context = null)
{
$defaultId = post('default');
if (Locations_model::updateDefault(['location_id' => $defaultId])) {
flash()->success(sprintf(lang('admin::lang.alert_success'),
lang('admin::lang.locations.alert_set_default')));
}
return $this->refreshList('list');
}
public function settings_onSave($context = null)
{
try {
$this->asExtension('FormController')->edit_onSave('edit',
params('default_location_id'));
return $this->refresh();
}
catch (Exception $ex) {
$this->handleError($ex);
}
}
public function listOverrideColumnValue($record, $column, $alias = null)
{
if ($column->type != 'button')
return null;
if ($column->columnName != 'default')
return null;
$attributes = $column->attributes;
$column->iconCssClass = 'fa fa-star-o';
if ($record->getKey() == params('default_location_id')) {
$column->iconCssClass = 'fa fa-star';
}
return $attributes;
}
public function formExtendQuery($query)
{
if ($locationId = $this->getLocationId())
$query->where('location_id', $locationId);
}
public function formAfterSave($model)
{
if (post('Location.options.auto_lat_lng')) {
if ($logs = Geocoder::getLogs())
flash()->error(implode(PHP_EOL, $logs))->important();
}
}
}
Views
Now the n views folder there is folder names menus and under that folder there is create.php file for displaying create menu form
The code in views->menus->create.php file is below
<div class="row-fluid">
<?= form_open(current_url(),
[
'id' => 'edit-form',
'role' => 'form',
'method' => 'POST',
]
); ?>
<?= $this->renderForm(); ?>
<?= form_close(); ?>
</div>
FormController
Now the renderForm() function is present at path app/admin/actions/FormController.php which we have defined in Locations and Menus controller under public $implement = ['Admin\Actions\FormController'];
Ther renderForm() function is as follow
public function renderForm($options = [])
{
if (!$this->formWidget) {
throw new Exception(lang('admin::lang.form.not_ready'));
}
if (!is_null($this->toolbarWidget)) {
$form[] = $this->toolbarWidget->render();
}
$form[] = $this->formWidget->render($options);
return implode(PHP_EOL, $form);
}
Widgets
At last the there are widgets for input fields like select, text, radio, checkbox etc. In our case we have widget name field_selectlist, which is present at path app/admin/widgets/form/field_selectlist.php
The field_selectlist.php file has code as below
<?php
$fieldOptions = $field->options();
//print_r($fieldOptions);die; All the locations are displaying here.
$isCheckboxMode = $field->config['mode'] ?? 'checkbox';
$selectMultiple = $isCheckboxMode == 'checkbox';
$checkedValues = (array)$field->value;
$enableFilter = (count($fieldOptions) > 20);
?>
<div class="control-selectlist">
<select
data-control="selectlist"
id="<?= $field->getId() ?>"
name="<?= $field->getName() ?><?= $selectMultiple ? '[]' : '' ?>"
<?php if ($field->placeholder) { ?>data-non-selected-text="<?=
e(lang($field->placeholder)) ?>"<?php } ?>
<?= $selectMultiple ? 'multiple="multiple"' : '' ?>
data-enable-filtering="<?= $enableFilter; ?>"
data-enable-case-insensitive-filtering="<?= $enableFilter; ?>"
<?= $field->getAttributes() ?>>
<?php if ($field->placeholder) { ?>
<option value=""><?= e(lang($field->placeholder)) ?></option>
<?php } ?>
<?php
foreach ($fieldOptions as $value => $option) { ?>
<?php
if (!is_array($option)) $option = [$option];
if ($field->disabled AND !in_array($value, $checkedValues)) continue;
?>
<option
<?= in_array($value, $checkedValues) ? 'selected="selected"' : '' ?>
value="<?= $value ?>">
<?= e(is_lang_key($option[0]) ? lang($option[0]) : $option[0]) ?>
<?php if (isset($option[1])) { ?>
<span><?= e(is_lang_key($option[1]) ? lang($option[1]) :
$option[1]) ?></span>
<?php } ?>
</option>
<?php } ?>
Using namespaces to create instances of database tables and controllers. When I try to load the 'RegisterController' I'm getting an error that 'Warning: require(../Furniture/Controllers/page.php): failed to open stream: No such file or directory in /srv/http/assignment/autoload.php on line 6'.
Find this odd as the instances of the database's are loading but the controller isn't?
//AutoLoad.php
<?php
function autoload($className)
{
$fileName = str_replace('\\', '/', $className) . '.php';
$file = '../' . $fileName;
require $file;
}
spl_autoload_register('autoload');
// Routes.php
<?php
namespace Furniture;
use Furniture\Conrollers\pageController;
class Routes implements \classes\Routes {
public function getRoutes(){
require '../classes/DatabaseConnection.php';
$furnitureTable = new \classes\databaseFunctions($pdo, 'furniture', 'id');
$categoryTable = new \classes\databaseFunctions($pdo, 'category', 'id');
$usersTable = new \classes\databaseFunctions($pdo, 'users', 'id');
$registerController = new \Furniture\Controllers\page($usersTable);
//$registerController = new \Furniture\Controllers\RegisterController($usersTable);
$routes = [
'' => [
'GET' => [
'controller' => $pageController,
'function' => 'home',
]
],
'about' => [
'GET' => [
'controller' => $pageController,
'function' => 'about',
]
]
];
return $routes;
}
}
//Pagecontroller.php
<?php
namespace Furniture\Controllers;
class page {
private $categoryTable;
public function __construct($usersTable)
{
$this->$usersTable = $usersTable;
}
public function home()
{
return ['template' => 'home-template.php',
'title' => 'This is the new home page',
'variables' => [
]
];
}
public function about()
{
return ['template' => 'about-template.php',
'title' => 'This is the new about page',
'variables' => [
]
];
}
public function faq()
{
return ['template' => 'faq-template.php',
'title' => 'This is the new faq page',
'variables' => [
]
];
}
public function contact()
{
return ['template' => 'contact-template.php',
'title' => 'This is the new contact page',
'variables' => [
]
];
}
public function furniture()
{
return ['template' => 'furniture-template.php',
'title' => 'This is the new furniture page',
'variables' => [
]
];
}
}
?>
//EntryPoint.php
<?php
namespace classes;
class EntryPoint {
private $routes;
public function __construct($routes){
$this->routes = $routes;
}
public function run() {
$route = strtolower(ltrim(explode('?', $_SERVER['REQUEST_URI'])[0], '/'));
$routes = $this->routes->getRoutes();
$method = $_SERVER['REQUEST_METHOD'];
$controller = $routes[$route][$method]['controller'];
$functionName = $routes[$route][$method]['function'];
$page = $controller->functionName();
$output = $this->loadTemplate('../templates/' . $page['template'], $page['variables']);
$title = $page['title'];
require '../templates/layout.html.php';
}
function loadTemplate($fileName, $templateVars) {
extract($templateVars);
ob_start();
require $fileName;
$content = ob_get_clean();
return $content;
}
}
As I undrestand the filename is Pagecontroller.php so please name your class accordingly to
Pagecontroller
//Pagecontroller.php
<?php
namespace Furniture\Controllers;
class Pagecontroller {
Alternative would be to rename your file to page.php as the error suggest it is looking for such a file
I have this code:
index.php
$router = new Core\Libraries\Router();
$router->add('', ['controller' => 'HomeAdminController', 'action' => 'index']);
$router->add('home', ['controller' => 'HomeAdminController', 'action' => 'indexAction']);
$router->add('home2', ['controller' => 'HomeAdminController', 'action' => 'indexAction2']);
$router->add('home3', ['controller' => 'HomeAdminController', 'action' => 'indexAction3']);
router.php
class Router
{
protected $_routes = [];
protected $_params = [];
public function add(string $route, array $params = [])
{
$route = preg_replace('/\//', '\\/', $route);
$route = preg_replace('/\{([a-z]+)\}/', '(?P<\1>[a-z-]+)', $route);
$route = preg_replace('/\{([a-z]+):([^\}]+)\}/', '(?P<\1>\2)', $route);
$route = '/^' . $route . '$/i';
$this->_routes[$route] = $params;
}
public function getRoutes(): string
{
return $this->_routes;
}
public function match(string $url): bool
{
foreach ($this->_routes as $route => $params) {
if (preg_match($route, $url, $matches)) {
foreach ($matches as $key => $match) {
if (is_string($key)) {
$params[$key] = $match;
}
}
$this->_params = $params;
return true;
}
}
return false;
}
public function getParams(): string
{
return $this->_params;
}
public function dispatch(string $url)
{
$url = $this->removeQueryStringVariables($url);
if ($this->match($url)) {
$controller = $this->_params['controller'];
$controller = $this->convertToStudlyCaps($controller);
$controller = $this->getNamespace() . $controller;
if (class_exists($controller)) {
$controller_object = new $controller($this->_params);
$action = $this->_params['action'];
$action = $this->convertToCamelCase($action);
if (is_callable([$controller_object, $action])) {
$controller_object->$action();
} else {
throw new \Exception("Method $action (in controller $controller) not found");
}
} else {
throw new \Exception("Controller class $controller not found");
}
} else {
throw new \Exception('No route matched.', 404);
}
}
protected function convertToStudlyCaps(string $string): string
{
return str_replace(' ', '', ucwords(str_replace('-', ' ', $string)));
}
protected function convertToCamelCase(string $string): string
{
return lcfirst($this->convertToStudlyCaps($string));
}
protected function removeQueryStringVariables(string $url): string
{
if ($url != '') {
$parts = explode('&', $url, 2);
if (strpos($parts[0], '=') === false) {
$url = $parts[0];
} else {
$url = '';
}
}
return $url;
}
protected function getNamespace(): string
{
$namespace = null;
$namespace = 'Controllers\\';
if (array_key_exists('namespace', $this->_params)) {
$namespace .= $this->_params['namespace'] . '\\';
}
return $namespace;
}
}
**controllers: Home.php**
class HomeAdminController extends Controllers\Controller
{
protected function before()
{
}
protected function after()
{
}
public function indexAction()
{
echo "## 1";
}
public function index()
{
echo "## 2";
$config = Registry::register("Core\Libraries\Config");
View::renderTemplate('Home/index.twig', [
'name' => 'Dave',
'colours' => ['red', 'green', 'blue']
]);
}
public function indexAction2()
{
echo "## 2";
$config = Registry::register("Core\Libraries\Config");
View::renderTemplate('Home/home2.twig', [
'name' => 'Dave',
'products' => [
[
'name' => 'Notebook',
'description' => 'Core i7',
'value' => 800.00,
'date_register' => '2017-06-22',
],
[
'name' => 'Mouse',
'description' => 'Razer',
'value' => 125.00,
'date_register' => '2017-10-25',
],
[
'name' => 'Keyboard',
'description' => 'Mechanical Keyboard',
'value' => 250.00,
'date_register' => '2017-06-23',
],
]
]);
}
public function indexAction3()
{
echo "home 3";
print_r($_POST);
View::renderTemplate('Home/home2.twig', [
'name' => 'Dave',
'colours' => ['red', 'green', 'blue']
]);
}
}
Controlers path: Controllers\Name.php
Is it possible to automatically build such routers?
I mean search the directory: Controllers \ and build for example based on files:
- Controllers \ home.php
- Controllers \ product.php
- Controllers \ contact.php
- Controllers \ productList.php
....
in an automatic way:
$ router-> add ('', ['controller' => 'HomeAdminController', 'action' => 'index']);
$ router-> add ('home', ['controller' => 'HomeAdminController', 'action' => 'indexAction']);
$ router-> add ('home2', ['controller' => 'HomeAdminController', 'action' => 'indexAction2']);
$ router-> add ('home3', ['controller' => 'HomeAdminController', 'action' => 'indexAction3']);
$ router-> add (product '', ['controller' => 'Product AdminController', 'action' => 'index']);
$ router-> add ('contact', ['controller' => 'ContactAdminController', 'action' => 'index']);
$ router-> add ('productList', ['controller' => 'ProductListAdminController', 'action' => 'index']);
I would like to automatically build this router so that I do not have to manually.
Does anyone know how to do it?
I am writing my own Auth component for CakePHP 2, which inherit from BaseAuthenticate. This component use an external lib (located in /usr/share/php) which save information in the $_SESSION variable.
My problem is that when I change of page, all this information is removed from $_SESSION, so the external lib do not sees that I am already connected.
I tried to save the content of $_SESSION added by the lib by using $this->Session->write in Auth.MyAuthenticateComponent, but this is also removed.
Thanks for your help.
edit :
The code :
class AppController extends Controller {
public $use = array('User');
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'Sheets', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'Users', 'action' => 'login')
)
);
public function beforeFilter()
{
$this->Auth->authenticate = array('Arise');
}
}
class UsersController extends AppController
{
public $helpers = array('Html', 'Form');
public function beforeFilter()
{
parent::beforeFilter();
$this->Auth->allow('login');
$this->Auth->authenticate = array('Arise');
}
public function login()
{
if ($this->request->is('post')) {
if ($this->Auth->login())
return $this->redirect($this->Auth->redirect());
else
$this->Session->setFlash('Error');
}
}
public function logout()
{
$this->redirect($this->Auth->logout());
}
}
App::uses('BaseAuthenticate', 'Controller/Component/Auth');
require_once('/usr/share/php/openid/consumer/consumer.php');
class AriseAuthenticate extends BaseAuthenticate
{
protected function _ariseAuthenticate($openid_url)
{
$consumer =& AriseOpenID::getInstance();
$required = array(
'http://somewhere/types/identifiant',
'http://axschema.org/namePerson/first',
'http://axschema.org/namePerson/friendly'
);
$consumer->setReturnTo('http://mysite/users/login');
$consumer->setTrustRoot('http://mysite/users/login');
$consumer->authenticate($openid_url, $required);
if ($consumer->isLogged()) {
$first_name = $consumer->getSingle('http://axschema.org/namePerson/first');
$nick = $consumer->getSingle('http://axschema.org/namePerson/friendly');
$id_arise = $consumer->getSingle('http://openid.iiens.net/types/identifiant');
return array(
'id_arise' => $id_arise,
'first_name' => $first_name,
'nick' => $nick
);
}
return false;
}
public function checkUser($result)
{
$User = ClassRegistry::init('User');
$result = $User->find('first', array(
'conditions' => array(
'id_arise' => $result['id_arise']
)
));
if (!$result) {
$User->create();
$User->save(array(
'id_arise' => $result['id_arise'],
'first_name' => $result['first_name'],
'nick' => $result['nick']
));
$result = $User->find('first', array(
'conditions' => array(
'id_arise' => $result['id_arise']
)
));
}
$user = $result['User'];
unset($result['User']);
return array_merge($user, $result);
}
public function authenticate(CakeRequest $request, CakeResponse $response)
{
if (!$request->is('post'))
return false;
$openid_url = (array_key_exists('login', $request->data))
? $request->data['login']['openid_url']
: NULL;
$openid_url = ($openid_url == '') ? NULL : $openid_url;
if ($result = $this->_ariseAuthenticate($openid_url))
return $this->checkUser($result);
return false;
}
public function getUser()
{
if ($result = $this->_ariseAuthenticate(NULL))
return $this->checkUser($result);
return false;
}
}
Are you unit testing your auth adapter? It is hard to tell what is wrong but I'm more or less sure authenticate() does not return the array but always false? Because if authenticate() returns valid user data it would be written to the session.
See http://api.cakephp.org/2.3/source-class-AuthComponent.html#543, that calls http://api.cakephp.org/2.3/source-class-AuthComponent.html#690 which calls all configured auth adapters. If your adapter returns an user array it should work. So I assume your adapter fails to return the array.
I thought I have to ask here some help to my problem. I've spend whole evening with this. I have a login method in UsersController like this:
public function login() {
if ( $this->request->is( 'post' ) ) {
if ( $this->Auth->login() ) {
$this->redirect( array( 'controller' => 'reservations', 'action' => 'index' ) );
} else {
$this->Session->setFlash( __( 'Login error.' ), 'flashError' );
}
}
}
I trying to test this with PHPUnit, so I can be sure that only valid users can login → after a successful login they will be redirected to a specific page. Here's my testLogin method in UsersControllerTest class:
function testLogin() {
$UsersController = $this->generate( 'Users', array(
'components' => array(
'Auth' => array( 'user' )
),
)
);
$UsersController->Auth->expects( $this->any() )
->method( 'user' )
->with( 'id' )
->will( $this->returnValue( 2 ) );
$data = array( 'User' => array(
'student_number' => 1111111,
'password' => 'qwerty'
) );
//$UsersController->Auth->login( $data['User'] );
$this->testAction( '/users/login', array( 'data' => $data, 'method' => 'get' ) );
$url = parse_url( $this->headers['Location'] );
$this->assertEquals( $url['path'], '/reservations' );
}
I am still learning the basics of unit testing with CakePHP. I get this error:
PHPUNIT_FRAMEWORK_ERROR_NOTICE
Undefined index: Location
Test case: UsersControllerTest(testLogin)
I have no idea what causes this... What's wrong with my test method and how it should be written?
Thanks!
I got this working with the following code:
function testLogin() {
//mock user
$this->Users = $this->generate( 'Users', array(
'components' => array(
'Security' => array( '_validatePost' ),
)
) );
//create user data array with valid info
$data = array();
$data['User']['student_number'] = 1234567;
$data['User']['password'] = '[valid password here]';
//test login action
$result = $this->testAction( "/users/login", array(
"method" => "post",
"return" => "contents",
"data" => $data
)
);
$foo[] = $this->view;
//debug($foo);
//test successful login
$this->assertNotNull( $this->headers['Location'] );
$this->assertContains( 'reservations', $this->headers['Location'] );
$this->assertNotContains( '"/users/login" id="UserLoginForm"', $foo );
//logout mocked user
$this->Users->Auth->logout();
}
I use this testcase to override the cake Auth call and Session and check if the login is successful.
this is more of a generic solution that i use in my testing., to get the values put into the session after the user logs in and also to check if the login is successful.
<?php
App::uses('UsersController', 'Controller');
App::uses('AuthComponent', 'Controller/Component');
App::uses('CakeRequest', 'Network');
App::uses('CakeResponse', 'Network');
$_SERVER['HTTP_USER_AGENT'] = '';
class stubSession {
public $data = array();
public function write($key, $value){
$this->data[$key] = $value;
}
public function read($key){
if(array_key_exists($key, $this->data)){
return $this->data[$key];
}
}
public function renew() {
}
public function setFlash(){
}
public function delete() {
}
public function check(){
}
}
class stubRequest {
public $data = array();
function __construct($data) {
$this->data = $data;
}
public function is() {
return true;
}
public function clientIp(){
}
}
class stubAuthComponent extends AuthComponent{
public static $_session;
public function __construct($args, $session){
parent::__construct($args);
$this->Session = $session;
self::$_session = $session;
$this->request = new CakeRequest();
$this->response = new CakeResponse();
}
public function loggedIn() {
return self::$_session->read(self::$sessionKey) != array();
}
public function logout(){
}
public static function user($key) {
$user = self::$_session->read(self::$sessionKey);
return $user[$key];
}
}
class UsersControllerTest extends UsersController {
function __construct($data){
$this->User = ClassRegistry::init('User');
$this->Session = new stubSession();
$this->Auth = new stubAuthComponent(new ComponentCollection(), $this->Session);
$this->request = new stubRequest($data);
}
public function redirect(){
}
}
class TestUsersController extends CakeTestCase {
public function testLogin(){
$_POST = array('User' => array('email' => 'mail#someemail.com', 'username' => 'mail#someemail.com', 'password' => 'abcd1234'));
$usersController = new UsersControllerTest($_POST);
$usersController->login();
$login = $usersController->Auth->loggedIn();
//debug($usersController->Session->data); //you can test the session key value in here
$this->assertEquals($login, true);
}
}