This entry is an extension to the current manual page for user authentication and is intended for folks who have already been through the basics of Solar. What I hope to accomplish here is to show you how to setup user authentication using MySQL, something the manual currently does not elaborate upon.
Getting Started - Configuring MySQL to work with Solar
First, we want to setup our configuration file to tell Solar to use the MySQL adapter so we can establish a connection to the database. We do this by defining some parameters for Solar_Sql and Solar_Sql_Adapter_Mysql. For more information on how to setup a configuration file please see the Config File manual page.
Your configuration file should look something like this. The first couple of settings are for the Solar environment (Solar_Uri_Action and Solar_Uri_Public), so you really only need to pay close attention to Solar_Sql and Solar_Sql_Adapter_Mysql here.
<?php
$config = array();
// Front controller HREF prefix
$config['Solar_Uri_Action']['path'] = '/index.php';
// Public directory HREF prefix
$config['Solar_Uri_Public']['path'] = '/public';
// Specify our SQL adapter
$config['Solar_Sql'] = array(
'adapter' => 'Solar_Sql_Adapter_Mysql',
);
/*
*
* SQL adapter information
*
* `host` : Host that houses our database.
* `user` : User name that has access to the database.
* `pass` : Associated password with the user name.
* `name` : Name of the database we are connecting to.
*
*/
$config['Solar_Sql_Adapter_Mysql'] = array(
'host' => 'localhost',
'user' => 'root',
'pass' => 'myS3cR37p@ssw0rd',
'name' => 'MyProject',
);
// Done!
return $config;
?>
Save your configuration file. Feel free to leave it open as we will be coming back to it shortly.
MySQL table creation
Next, we want to create the table that will hold our user information. At this point I am assuming that you have already created your database and the proper credentials associated with it. If not, please do so now. Take note that this is a very simplistic table and is not optimized in anyway.
# Basic user table layout
#
# name : Real name of our user.
# handle : User name used to login with.
# password : MD5 hash will be stored here.
CREATE TABLE users(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
name VARCHAR(255),
handle VARCHAR(255),
password VARCHAR(32)
)
Configuring Solar authentication to work with MySQL
Now that we have our users table, we need to jump back into our configuration file and tell Solar to use the SQL adapter for authentication. This is what our final configuration file will look like.
<?php
$config = array();
// Front controller HREF prefix
$config['Solar_Uri_Action']['path'] = '/index.php';
// Public directory HREF prefix
$config['Solar_Uri_Public']['path'] = '/public';
// Specify our SQL adapter
$config['Solar_Sql'] = array(
'adapter' => 'Solar_Sql_Adapter_Mysql',
);
/*
*
* SQL adapter information
*
* `host` : Host that houses our database.
* `user` : User name that has access to the database.
* `pass` : Associated password with the user name.
* `name` : Name of the database we are connecting to.
*
*/
$config['Solar_Sql_Adapter_Mysql'] = array(
'host' => 'localhost',
'user' => 'root',
'pass' => 'myS3cR37p@ssw0rd',
'name' => 'MyProject',
);
// Specify our authentication adapter
$config['Solar_Auth'] = array(
'adapter' => 'Solar_Auth_Adapter_Sql',
);
/*
*
* Authentication adapter information
*
* `table` : MySQL table holding our user information.
* `handle_col` : Column for the user name.
* `passwd_col` : Column for the user's password.
*
*/
$config['Solar_Auth_Adapter_Sql'] = array(
'table' => 'users',
'handle_col' => 'handle',
'passwd_col' => 'password',
);
// Done!
return $config;
?>
Done! - Conclusion and additional resources
That’s it! It really is that simple to setup user authentication using MySQL and Solar. If you got lost in the process, please reference the materials below. If you are still stuck, please join the Solar mailing list and/or IRC channel and post up. We would love to hear from you.
- Solar Manual - The official Solar manual.
- Config File Help - Working with Solar’s configuration file.
- Solar_Sql and Solar_Sql_Adapter_Mysql - API references
Comments
Hi everyone!
I wrote about Setting Up SQL-based User Authentication with Solar on my blog a while ago. You should check that out too :-)