Friday, May 6, 2011

Finishing the Coursework

Finally the coursework is over!

The realization of this coursework and reporting my progress inside this public blog has benn a great experience and I have found quite interesting to develop application with CAKEPHP. I think this is only the begging for me becouse I am very interested on going on with my research and development of cakePHP application.

Angel Caballero

Why reg as a role for the user?

If someone looks inside the database or when he sign in my blog he will become a registered user and his role is going to be "reg".
The reason to use a role by default is to denny or allow accesebility to the controllers using that role. As for example in the post controller we can write this code :

function beforeFilter() {
parent::beforeFilter();
$this->Auth->deny('add','edit','delete');
if ( $this->Auth->user('roles')=='admin')
$this->Auth->allow('add','edit','delete');
else if ($this->Auth->user('roles')=='reg')
$this->Auth->allow('add');
}
That means that the admin is going to be able to add, edit and delete and the registered user only to add. But if you are a non-registered user you won't be able to add, edit or delete.
When you sign in this role is associated by default and the registered user can't change it. The only user that can choose the role when he is adding a new user or editing a exisiting one is the admin. 

Route

By default, CakePHP responds to a request for the root of your site (i.e. http://www.example.com) using its PagesController, rendering a view called "home". Instead, we'll replace this with our PostsController by creating a routing rule.


In my blog application I want to acced directly to the post view so I have to change the default route : 


Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
In order to be the post controller and the action has to be display the index page so, the line now it's gonna be like this :


Router::connect('/', array('controller' => 'posts', 'action' => 'display', 'index')); 


Now, we can access to our blog just with the address : http://127.0.0.1/uscms 

Thursday, May 5, 2011

Finishing the CRUD

After all the changes apply to the blog, the database and the CRUD structure is like the following table :

  • POST :  
      • C: User, Admin
      • R: User, Admin, Visitor
      • U: Admin
      • D: Admin
  • COMMENTS: 
      • C: User, Admin
      • R: User, Admin, Visitor
      • U: Admin, User
      • D: Admin
  • USERS:
      • C: User, Admin , Visitor
      • R: User, Admin, Visitor
      • U: Admin, User
      • D: Admin
We have selected this funcionalies becouse I think they are the best for the best behaviour of the blog.

File Upload

In our blog we allow that a regular user or a admin can upload a file when he is creating a new post. But we want to put some restrictions.
We don't want the user to upload a file without extension, but furthermore we only are going to allow the user to upload a image file.
That's why he have include this rule inside our post method :

'file_name' => array(
  'file_name' =>array(
   'rule' => array('extension', array('gif', 'jpeg', 'png', 'jpg')),
   'message' => 'The file has to be a image file'
   )
   )
That rules indicate that the filename has to have the extension of a image file. That's one of our procedure to validate the data
 

Validation

Inside our blog I have include a page where the user can registered and the admin can also add new users if they want.
Inside the sign in form I have include some validation rules such as :

The name, username and the password has to exist. The form don't admint empty fields.
The username has to be between 4 and 10 characters
The password has to be confirmed and has to match


To do this last point I had to overwrite the hashfunction and also a function to check if the password introduced in the password field is the same than the password introduced in the confirm password field. All of this methods has been impleted inside the user model. 

Different Actions

In this post I am going to show an example of how the same page can show different actions depending on the role of the user.

 This first view show us the page of the comment (view page) for a unrigestered user. As we can see, that kind of user only can show the posts.
 This second image show us the view for a registered user. We can see that much more actions are availables.
The last image correspond to the view of the admin. The difference between this view and the view of the registered user is that the admin can add a new user and delete comments.