I have a problem with using Laravel built in authentication feature. As a default Laravel authentication access users table to check/add username and password. I need to change it to student table. normally in a model protected $table=student code is used to mention which table to use.
Can anyone tell me where the protected $table= code or similar is found within the built in authentication feature?
Part 2
The code below is the code in my controller where I take the form data into $data and validating and returning it into my store function, but I get an error
Type error: Too few arguments to function App\Http\Controllers\StudentController::store(), 0 passed and exactly 1 expected"
protected function validator(array $data)
{
$data = Request::all();
return Validator::make($data, [
'fname' => 'required|string|max:255',
'lname' => 'required|string|max:255',
'district' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:student',
'password' => 'required|string|min:6|confirmed',
]);`
}
public function store(array $data)
{
return Student::create([
'fname' => $data['fname'],
'lname' => $data['lname'],
'district' => $data['district'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
Add it in the App\User.php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $table = "users_old"; //your custom table
....
}
Add:
protected $table=student;
to User model at app/User.php
and
In app/Http/Controllers/Auth/RegisterController.php,
Change:
'email' => 'required|email|max:255|unique:users',
To:
'email' => 'required|email|max:255|unique:student',
Related
I am trying to switch to and query a external database in a custom Drupal 8 module I have created.
I have added the external database below the native database in settings.php:
// Add second database
$databases['external']['default'] = array(
'database' => 'uconomy_external',
'username' => 'uconomy_admin',
'password' => 'fNjA9kC35h8',
'prefix' => '',
'host' => 'localhost',
'port' => '3306',
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'driver' => 'mysql',
);
I then have a file named BusinessListingDbLogic.php where I make queries to the database :
<?php
namespace Drupal\business_listing;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Database;
/**
* Defines a storage handler class that handles the node grants system.
*
* This is used to build node query access.
*
* This class contains all the logic for interacting with our database
*
* #ingroup business_listing
*/
class BusinessListingDbLogic {
/**
* #var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* #param \Drupal\Core\Database\Connection $connection
*/
public function __construct(Connection $connection) {
$this->database = $connection;
//Database::setActiveConnection('external');
}
/**
* Add new record in table business_listing.
*/
public function add($title, $body, $imageName, $location, $email) {
if (empty($title) || empty($body) || empty($imageName) || empty($location) || empty($email)) {
return FALSE;
}
// add record to business_listing table in database.
$query = $this->database->insert('business_listing');
$query->fields(array(
'title' => $title,
'body' => $body,
'image' => $imageName,
'location' => $location,
'email' => $email
));
return $query->execute();
}
I believe my BusinessListingDbLogic class is registered as a service, my business_listing.services.yml looks as follows:
services:
# Service Name.
business_listing.database.external:
class: Drupal\Core\Database\Connection
factory: 'Drupal\Core\Database\Database::getConnection'
arguments: ['external']
# external database dependent serivce.
business_listing.db_logic:
# Class that renders the service.
# BusinessListingDbLogic contains all the functions we use to interact with the business_listings table
class: Drupal\business_listing\BusinessListingDbLogic
# Arguments that will come to the class constructor.
arguments: ['#business_listing.database.external']
# A more detailed explanation: https://www.drupal.org/node/2239393.
# tags:
# - { name: backend_overridable }
This code works until I try uncomment Database::setActiveConnection('external');
I then get the following error:
The website encountered an unexpected error. Please try again later.Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'uconomy_external.shortcut_set_users' doesn't exist: SELECT ssu.set_name AS set_name
FROM
{shortcut_set_users} ssu
WHERE ssu.uid = :db_condition_placeholder_0; Array
(
[:db_condition_placeholder_0] => 1
)
it looks like the switch is working, but Drupal might be trying to use the external database for its native functionality? I know I also have to switch back to the default database at some point, but I am not sure where to do this?
Any help or advice would be GREATLY appreciate. Kind Regards, Matt
Seems that instead of calling your current connection to set, you need to use the static method Database::setActiveConnection() directly.
Eg. $this->database->setActiveConnection('external') becomes Database::setActiveConnection('external')
I have an existing website (functional) and now i need to upgrade my website by cakephp and also, import old DB to new DB.
Cakephp have default Algorithm for hash and password for that i need to change Algorithm .
My old website used this code for password:
$password_hash = hash('sha256', $password);
How can I set cakephp password hash auth like: hash('sha256', $password) until my website users can login into cakephp script?
please help...
cakephp ver: CakePHP(tm) v 0.2.9<br><br>
note: apologize For the weak English
I assume you are using CakePHP 3.x which uses the bcrypt hashing algorithm by default.
To use sha256 hasing you can create custom password hasher class.
namespace App\Auth;
use Cake\Auth\AbstractPasswordHasher;
class Sha256PasswordHasher extends AbstractPasswordHasher
{
public function hash($password)
{
return sha256($password);
}
public function check($password, $hashedPassword)
{
return sha256($password) === $hashedPassword;
}
}
and configure the AuthComponent to use your own password hasher:
public function initialize()
{
parent::initialize();
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'passwordHasher' => [
'className' => 'Sha256',
]
]
]
]);
}
read more here https://book.cakephp.org/3.0/en/controllers/components/authentication.html#hashing-passwords
SHA3-512 is not supported in cakephp version 2.x, and in cakephp version 2.x we can use at max SHA-512.
You can do the same by making changes in app/Controller/AppController.php by adding below patch,
$this->Auth->authenticate = array(
'Form' => array(
'passwordHasher' => array(
'className' => 'Simple',
'hashType' => 'sha512' //passing sha512 as the hash type
)
)
);
If you are giving a option to change/reset password after updating your hash, you may use below patch to accept password with updated hash,
$var = Security::hash($password, 'sha512', true);
Here sha512 hash algorithm will be used, you can change it as per your hash requirement(sha1/sha256/md5/blowfish), if salt value i.e. third parameter is set to true application's salt value will be used.
I read all the cakephp component Auth documentation in http://book.cakephp.org/3.0/en/controllers/components/authentication.html but I cant find a solution :-(
I'm trying to use different model called "Usuarios" in component Auth and change the field "username" by "cedula". This is my configuration in AppController.php:
public function initialize() {
parent::initialize();
$this->loadComponent('Flash');
$this->loadComponent('Auth');
$this->Auth->config('authenticate', [
'Basic' => [
'userModel' => 'Usuarios',
'fields' => [
'username' => 'cedula',
'password' => 'password']],
'Form' => ['userModel' => 'Usuarios',
'fields' => [
'username' => 'cedula',
'password' => 'password']]
]);
But nothing happend. No appear login form and session is open.
What i'm doing wrong?
Match the controller name to the template directory, and the method to the template.
If your controller is src/Controllers/Usuarios, method login(), then in src/Templates/Usuarios/ you need to have login.ctp as your view file.
If you have all that, it should work. By the way, if you are using 'password' as your password field, you don't need to specify that when you configure Auth; You only need to set 'username' => 'cedula' because you are changing the default.
Also, have you set anything in $this->Auth->allow() in the beforeFilter() method?
I'm building an application using CakePHP and trying to incorporate a custom authentication object but it does not seem to be able to find it. I get the following error when I try to log in: "Authentication adapter "LdapAuthorize" was not found". I have created the file app/Controller/Component/Auth/LdapAuthorize.php with my code for my authentication. Near the top of "AppController.php" I have
App::uses('LdapAuthroize', 'Controller/Component/Auth/LdapAuthorize');
and within the AppController class I have
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'pendings', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
'authorize' => array('Controller'),
'authenticate' => array('LdapAuthorize')
)
);
and then in my UsersController.php I have the following login function.
public function login() {
if($this->request->is('post')) {
if($this->Auth->login()) {
// My Login stuff...
}
else
$this->redirect(array('controller'=>'someController', 'action'=>'someAction'));
}
}
If anyone has any idea why it can't seem to load my custom authentication object that would be awesome. Thanks!
I put my custom authentication class inside Controller/Component/Auth. For example, the name of my class is CustomUserAuthenticate and the path to the file is,
Controller/Component/Auth/CustomUserAuthenticate.php.
Then in my AppController I added the following to the authenticate array,
class AppController extends Controller {
public $components = array(
'Auth' => array(
/** Any other configuration like redirects can go here */
'authenticate' => array(
'CustomUser'
)
)
);
}
The string in the authenticate array must match the name of the class except for the Authenticate word.
My CustomUserAuthenticate class extends CakePHP's Controller/Component/Auth/BaseAuthenticate and overrides the authenticate method. CakePHP's documentation states that this is not required. I haven't tried that way.
I think your App::uses() is wrong so it can't find the class. Your current code:
App::uses('LdapAuthroize', 'Controller/Component/Auth/LdapAuthorize');
Is trying to find Controller/Component/Auth/LdapAuthorize/LdapAuthroize.php
The first parameter is the class name (you have a typo with that), the second is just the path to the directory containing the class, you don't need to add the class name again.
Try this:
App::uses('LdapAuthorize', 'Controller/Component/Auth');
Im getting the following error when trying to log in a user:
Call to a member function login() on a non-object
My login action uses an external model. The action is:
public function login() {
$this->loadModel('User');
if ($this->request->is('post')) {
$theUser = $this->User->find('first', array('conditions' => array('User.username' => $this->request->data['User']['username'])));
if($theUser['User']['activated'] == TRUE){
if ($this->Auth->login($this->request->data)){
$this->Session->setFlash('Logged in successfully');
$this->redirect(array('controller' => 'admin', 'action' => 'index'));
} else {
$this->Session->setFlash('Username or password is incorrect');
}
}else $this->Session->setFlash('User not yet activated. Please Contact administrator.');
}
}
The request data being passed to the $this->Auth->login is:
array(
'User' => array(
'password' => '*****',
'username' => 'admin'
)
)
$this->Auth->login($this->request->data) is the line causing the fatal error.
Can someone please explain what exactly the error means and what might be causing it?
You'll need to check that you've included the Auth component in the controller. For example:
class UsersController extends AppController {
public $components = array(
'Auth'
);
public function login() { ... }
}
As pointed out by #thaJeztah - check the docs for correct usage, as your code (which based on the usage of $this->request, implies you're using 2.x) is not correct and will not test if the user exists and can login - but instead directly log the user in whatever they put in the login form.