2

I'm busy for some time with a login system in CakePHP.

I have a website where people can join and login. What I want is if the users login for the first time the user has to proceed certain steps with information that must be completed.

I was thinking to create in my database a field active 1 or 0. When the user proceed the steps the profile has been activated and never show's the first time logged in page.

Such as Welcome 'User', My profile information -> Connect with us -> Information about the application
-> Activate your Account

Can anyone give me some snippets to do this.

Many Thanks!

  <?php

    class UsersController extends AppController {

        var $name = 'Users';

        function beforeFilter() {
            parent::beforeFilter();

            // Rights for the admin users 
            $this->Auth->mapActions(
                array(
                    'create' => array('admin_add'),
                    'read' => array('index', 'admin_index')
                )
            );

            // Everybody can login
            $this->Auth->allow(array('login'));

            // Apply ucfirst (Capital) & strolower (small text) to the username
            if (isset($this->data['User']['username'])) {
                $this->data['User']['username'] = ucfirst(strtolower($this->data['User']['username'])); 
            }

        }

        function login() {

            // Check if the user is logged in correctly, then update the date/time from this login
            if ($this->Auth->User()) {

                // Update the login date
                $this->User->save(array(
                    'id' => $this->Session->read('Auth.User.id'),
                    'last_login' => date('Y-m-d h:i:s')
                )); 

                // Get the group name
                $group = $this->User->Group->findById($this->Session->read('Auth.User.group_id'));


                // Set the redirect if the user has logged in as Administrator
                if ($group['Group']['name'] == 'Admins') { 
                    $this->redirect(array('controller' => 'posts', 'action' => 'index'));
                }
            }


            // Always redirect the user to the homepage 
    //      $this->redirect(array('controller' => '', 'action' => ''));


            pr($this->Session->read('Auth'));

        }


        function logout() {
            $this->redirect($this->Auth->logout());
        }

        function admin_index() {
            echo 'It Work's';
        }   

2 Answers 2

2

Use the active field for when they have completed the welcome process. However, you will need something to track each step of the process as well. What if they got to page two of the process and their computer shuts off?

I would have a table that tracks progress. Each step is added as the new user completes it. When all steps are complete, update the ACTIVE field in record in the USER table (indicating they completed all steps) and then remove their progress information from the table that tracks it.

The next time the user logs in, it will see that they have the ACTIVE flag set, and skip the welcome process. However, if they only got partway through, the ACTIVE field will still reflect 0 and the progress table will tell you right where they left off.

0

Yes, you could create a field on the users table.. and you could use the beforeFilter() method on your AppController to check if the user is conected and the activation field is set to 1 or 0. If the user is not activated, redirect to your form or whatever you need to do.. once everything is correct change the active field to 1... and that's it

Just be careful in the beforeFilter() method that you dont have to redirect the user if the current page is you activation form's page.. because it will create an infinite loop of redirections

Good Luck!

2
  • Thanks for your detailed explanation! This really helps in the development of this system. I have no idea where I should start in my code. Should I create a new controller with these features? I've created a new table in which each step is stored. These steps are then linked to the user_id in the user table Has anyone snippets for creating this system .. or anny example. Manny thanks !
    – NDeveloper
    Apr 29, 2011 at 14:54
  • there's a main controller called AppController book.cakephp.org/view/957/The-App-Controller that you could use to put code that is common for all controllers. if you followed the Auth tutorial on the doc, you should already have you own AppController with the beforeFilter(), you'll just need to check if the user is connected and his "activate" status.. I dont know if there is some snippets or code to do that "step"'s thing, it seems like you'll need to code that =) cheers! Apr 29, 2011 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.