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. 

Allowing actions

One of the main goals of this Coursework is define the permissions of each of the users depending of their roles. We have three kind of roles:
Admin
Registered User
Non Registered User

So, in order to allow certain actions to this users we have to first change the controller of each of the pages. Comments, Posts and Users. To explain this in this entry we are going to put the post as an example, for the rest the way to do it is quite similar.

First of all I have to change the beforeFilter function to allow the actions we want for each of the user. For the post we are going to allow both the admin and the registered user to add, edit and for all the users the view. Just for the admin is going to be the action to delete post. After changing this in the beforefilter function we have to define the authorization inside each of the functions (which are actions in our application) the visibility. Finally to show the actions to the users we have to change each of the views of the post folder, index, add, edit, delete....

Layout and CSS

One of the problematic points when I was developing the CMS BLOG with CakePHP was to define the tittle of the pages, and the layout and css uniforms.
CakePHP give us the posibility to change it in a intuitive way. First of all you have to add some code in the actions of the controller :

$this -->set ('title_for_layout', 'Name of the Blog');
This give us the posibility to use the Name of the Blog as the title of the page. Then we have to change the layout in the view folfer. If we name that layout default.ctp we don't need to specify that layout inside the controllers.

Inside the layout, default.ctp, we can indicate that we are going to use a CSS file. In that file we can specify the style of our blog.

Wednesday, May 4, 2011

The initial Structure

The application I am developing is a CMS content. The first schema of this application is very simple, we have two roles of users :
    Admin: Can create, edit and delete his own posts and comments and he also can create, edit and delete the other users posts and comments


Normal User: As it can be seen in the image of the left, when a normal user try to edit or delete a post he is not authorised. He only can delete or edit his own posts.


That is the main schema of the first working application made in cakephp

The first steps

The first steps in the creation of a CakePHP app is to install the Cake inside the xampp directory and make it work.
Following any of the tutorial available in Internet or even in the blackboard site of the University of the West of Scotland that is a simple process of only 5 minutes. Once the cakephp is working the next step is creating the database for our application. In this case the database is called : cakephpuser_uscms and it is going to be a simple CMS at the beginning.

Tuesday, April 12, 2011

First Post!

Hello everyone!
My name is Angel Caballero from the University of the West of Scotland and I have created this blog in order to record my progress of the second coursework of the module Dynamic Web Technologies. This courseworks is Web Framework in which I have to develop a web site using CAKEPHP web application framework.
During the next days I will be updating this blog to show all my progress on this coursework