0

I've been asked to use mysql users to login in a cakephp application.

I want to use the username and password from a login form and use it like this in the database.php config file.

class DATABASE_CONFIG {  

    public $default = array(
        'datasource' => 'Database/Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => $user,
        'password' => $password,
        'database' => 'cakephp',
        'prefix' => '',
        //'encoding' => 'utf8',
    );
}

To use the existing users in the mysql database, if it is possible this way. If it´s not, is there any other way to do this??

I´ve just found this to add to the class:

function __construct(){
    $this->default['login'] = $userSentFromForm;
    $this->default['password'] = $passwordSentFromForm;
}

But i don't get how to retrieve the login and password from the form and send it to the constructor in the $userSentFromForm and $passwordSentFromForm variables.

hug0
  • 511
  • 3
  • 17
  • 1
    Instead of defining them in the class header, you would probably be best to use the constructor and pass the username and password to the database_config class when instantiated. – Devon Bessemer Jun 05 '14 at 18:08

2 Answers2

0

To do that, a MySQL user account has to be created for each user of the site. You can add MySQL users and define privileges for them by executing SQL statements against the database but this has to be done as the MySQL 'root' user.

Consult the docs on this here.

As you normally don't want to access the database as root, this would require a separate connection to the database as root only when the register form is processed to create a database user for each new registration.

marekful
  • 14,986
  • 6
  • 37
  • 59
  • Thanks all for de answers, i'll look into the documentation you recommended me and see what can i do. Thanks again. – hug0 Jun 05 '14 at 18:56
0

For that, you will have to do a custom authentication components that inherits from Auth. This only needs to extend one method, authenticate(), and return data for the user if it logins, or false if it doesn't.

You can check more info here: http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#creating-custom-authentication-objects

As for checking if the user is a MySQL user, check the mysql.user table. Full process is here: How do I retrieve my MySQL username and password?

Community
  • 1
  • 1
Roberto Maldonado
  • 1,585
  • 14
  • 27