Welcome Guest, Not a member yet? Register   Sign In
Tank Auth v1.0 (CI authentication library)
#61

[eluser]theshiftexchange[/eluser]
Why does the Tank_Auth library force my user to be activated to use the 'forgot_password' option?

The reason I ask is that you allow a user who is not activated to "send_again()" the activation email, or even select a new email for activation.

Therefore, it is plausable that a user forgets their password when they are not activated; there is no harm in re-sending the email to the original email.

The code from the tank_auth library is below:

Code:
function forgot_password($login)
    {
        if (strlen($login) > 0) {
            if (!is_null($user = $this->ci->users->get_user_by_login($login, TRUE))) {

                $data = array(
                    'user_id'        => $user->id,
                    'username'        => $user->username,
                    'email'            => $user->email,
                    'new_pass_key'    => md5(rand().microtime()),
                );

                $this->ci->users->set_password_key($user->id, $data['new_pass_key']);
                return $data;

            } else {
                $this->error = array('login' => 'auth_incorrect_email_or_username');
            }
        }
        return NULL;
    }

All you need to do is

Code:
if (!is_null($user = $this->ci->users->get_user_by_login($login, TRUE))) {
should be changed to
Code:
if (!is_null($user = $this->ci->users->get_user_by_login($login, $someconfigvalue))) {
and allow $someconfigvalue to be set in the tank_auth config file if we want to allow non-activiated users to use the forgot_password function.

Cheers - and thanks again for all your hard work!

edit: you will also need to use the same variable in the "reset_password" function - as it has activated hardcoded to TRUE aswell.

I've 'hacked/modified' your library to hardcode both values to NULL, and it works perfectly for me as intended.
#62

[eluser]theshiftexchange[/eluser]
one last idea/request:

There should be a getUserEmail() function in the library. Because if you choose to NOT use a username, its because you are using the email as your login instead (which Tank Auth lets you decide in the config file).

EDIT: Ok - I looked into this a bit more. You'll need to add the user email to the session, which is a good thing if people like myself are ONLY using emails - not usernames.

on line 70 of tank_auth library:
Code:
$this->ci->session->set_userdata(array(
                                'user_id'    => $user->id,
                                'username'    => $user->username,                              
                                'status'    => ($user->activated == 1) ? STATUS_ACTIVATED : STATUS_NOT_ACTIVATED,
                        ));


just add
Code:
$this->ci->session->set_userdata(array(
                                'user_id'    => $user->id,
                                'username'    => $user->username,
                                'email'     => $user->email,
                                'status'    => ($user->activated == 1) ? STATUS_ACTIVATED : STATUS_NOT_ACTIVATED,
                        ));

then add
Code:
function get_email()
    {
        return $this->ci->session->userdata('email');
    }
#63

[eluser]Benely[/eluser]
Hey,

Love the system Smile but I have come across something and I don't know how to do it.

I want it so I can add an extra field on registration, I want it storing in the user_profile table, but where abouts is this done for the other fields in registration method?

Thanks Smile
#64

[eluser]theshiftexchange[/eluser]
[quote author="Benely" date="1243018424"]Hey,

Love the system Smile but I have come across something and I don't know how to do it.

I want it so I can add an extra field on registration, I want it storing in the user_profile table, but where abouts is this done for the other fields in registration method?

Thanks Smile[/quote]

In the auth.php file, go to the Register() function. You set the validation rules for all your fields, including the 'additional ones'. Then in the run() if statement, after the create_user function is called, you then add some lines to add the extra fields to your user_profile section.
#65

[eluser]millo[/eluser]
Hey, Thought I would replace my redundant question with something useful. I have successfully installed Tank Auth on my Windows Xampp server and thought I would post a copy of my log here in case anyone comes up against the same problems that I encountered.

Hope this is of help to somebody! :-) ...

### 30th May 2009 18:30 - Install Tank Auth Authentication ###

Researched the available authentication packages available. Narrowed it down to about four (The Auth Library, DX Auth, Tank Auth (a reworked DX Auth?) and Redux). It got down to Redux and Tank Auth because they seemed to be more comprehensive with regards to it's features and they were both meant to be easy to install. Finally I decided on Tank Auth. I just seemed to like the responsiveness and enthusiasm of it's developer on the forums.

http://ellislab.com/forums/viewthread/110993/
http://codeigniter.com/wiki/Tank_Auth/
http://konyukhov.com/soft/tank_auth/

Installing was easy enough.

1. Download the latest version of the library.
2. Unzip the package.
3. Copy the application folder content to your CI application folder.
4. Copy the captcha folder to your CI folder. Make sure this folder is writable by web server.
5. Install database schema into your MySQL database.
6. Open the application/config/config.php file in your CI installation and change $config['sess_use_database'] value to TRUE.

To add to point 5. I also had to set the database settings in config/database.php.

When I installed the Auth Library, the first thing I got was this error:

---
Code:
Object not found!
The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.
If you think this is a server error, please contact the webmaster.

Error 404
clienta.local
05/30/09 20:51:23
Apache/2.2.11 (Win32) DAV/2 mod_ssl/2.2.11 OpenSSL/0.9.8i PHP/5.2.9
---

I then thought that perhaps it had to do with the fact that I didn't have the .htaccess file in there so I got one out of an old project and put it in and it was fine.

It talks about the .htaccess settings here:
http://ellislab.com/codeigniter/user-gui.../urls.html

Here is a copy of what I put in my .htaccess file. I used this setting when working on a previous project. It's used to remove the index.php from the url and also so that I can reference files that are in folders like '/css' and '/img' much more easily. I think I'd gotten most of the settings below from a forum but not sure which one as it was a while ago for another project. You might have to look for it if it's necessary.

Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$  index.php/$1 [L]
</IfModule>

Some useful info on how to create overall file structure here:
http://ellislab.com/forums/viewthread/57602/

After having installed the .htaccess file, the example forms provided by Tank Auth started rendering fine. However a few initial problems that I noted:

1. The font on the captcha image was too small.
2. When registering it tried to send off an email and running in XAMPP/Windows it seemed to be an issue.

I solved both of them via this forum thread:
http://ellislab.com/forums/viewthread/110993/

- Solving Problem 1:

In system/application/config/tank_auth.php I made the following changes:

Code:
$config['captcha_fonts_path'] = 'captcha/fonts/3.ttf';
(this was using 5.ttf but font 3 seems clearer and characters more distinguishable)
Code:
$config['captcha_font_size'] = 18;
(this was set to 14 before, although it didn't increase much in size by setting it to 18 it did appear clearer)

- Solving Problem 2:

In the thread mentioned above there is a link to the following page that solved problem number 2.
http://i.justrealized.com/2008/05/02/how...-in-xampp/

These are what I changed the default settings in the following files:

C:\xampp\sendmail\sendmail.ini:
Code:
smtp_server=mail.myserver.net
[email protected]
auth_password=mypassword (make sure this works by testing your mail server to see that it accepts user and pass correctly)

C:\xampp\php\php.ini
Code:
SMTP = mail.myserver.net
sendmail_from = [email protected]
sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"

Remember! This is running off my laptop, Windows system so normally there is no mail server running, but this all seems to work now! Activation emails are being sent to me, even though gmail is classifying them as spam. I would imagine that's something that I can configure when it goes live.

- Change to schema.sql file:
I also modified the schema.sql file, it had the COLLATE settings as COLLATE utf8_bin and I changed them to COLLATE utf8_general_ci as that is what my general database collation is set at so that seems the appropriate thing to do.

Hope that this is useful to somebody.

Thanks Gromozeka! Nice Work!

--- end
#66

[eluser]millo[/eluser]
Hi Gromozeka

I hope that you can help me. I've been trying to extend the code so that I can add more fields into the user_profile database. Here is what I've done:

Added the extra fields into the database:

Code:
CREATE TABLE IF NOT EXISTS user_profiles (
    id int(11) NOT NULL AUTO_INCREMENT,
    user_id int(11) NOT NULL,
    country varchar(32) COLLATE utf8_general_ci DEFAULT NULL,
    first_name varchar(32) COLLATE utf8_general_ci NOT NULL default '',
    last_name varchar(32) COLLATE utf8_general_ci NOT NULL default '',
    contact_phone varchar(32) COLLATE utf8_general_ci NOT NULL default '',
    contact_email varchar(64) NOT NULL default '',
    web_url varchar(255) COLLATE utf8_general_ci NOT NULL default '',
    live tinyint(1) NOT NULL DEFAULT '0',
    PRIMARY KEY (`id`)
) ENGINE=InnoDB    DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

Just tried experimenting first so thought I would try and get it to save just 'country' for first attempt.

Added the necessary code into /views/auth/register_form.php and then also in the file /controllers/auth.php I added the necessary form validation code. That all ran fine up to there. I then modified the create_user function in libraries/Tank_auth.php:

Code:
/**
     * Create new user on the site and return some data about it:
     * user_id, username, password, email, new_email_key (if any).
     *
     * @param    string
     * @param    string
     * @param    string
     * @param    bool
     * @return    array
     */
    function create_user($username, $email, $password, $country, $email_activation)
    {
        if ((strlen($username) > 0) AND !$this->ci->users->is_username_available($username)) {
            $this->error = array('username' => 'auth_username_in_use');

        } elseif (!$this->ci->users->is_email_available($email)) {
            $this->error = array('email' => 'auth_email_in_use');

        } else {
            // Hash password using phpass
            $hasher = new PasswordHash(PHPASS_HASH_STRENGTH, PHPASS_HASH_PORTABLE);
            $hashed_password = $hasher->HashPassword($password);

            $data = array(
                'username'    => $username,
                'password'    => $hashed_password,
                'email'        => $email,
                'country'    => $country,
                'last_ip'    => $this->ci->input->ip_address(),
            );

            if ($email_activation) {
                $data['new_email_key'] = md5(rand().microtime());
            }
            if (!is_null($res = $this->ci->users->create_user($data, !$email_activation))) {
                $data['user_id'] = $res['user_id'];
                $data['password'] = $password;
                unset($data['last_ip']);
                return $data;
            }
        }
        return NULL;
    }

and in models/tank_auth/users.php I modified the create_user() and create_profile(). I modified the information so that it goes into a data array and then extract it again when it's passed.

Code:
/**
     * Create new user record // MODIFIED BY MILLO
     *
     * @param    array
     * @param    bool
     * @return    array
     */
    function create_user($data, $activated = TRUE)
    {
        // seperate out the user data and the user profile data

        // user data
        $user_data['username'] = $data['username'];
        $user_data['password'] = $data['password'];
        $user_data['email'] = $data['email'];
        $user_data['last_ip'] = $data['last_ip'];
        $user_data['new_email_key'] = $data['new_email_key'];
        $user_data['created'] = date('Y-m-d H:i:s');
        $user_data['activated'] = $activated ? 1 : 0;

        if ($this->db->insert(self::TABLE, $user_data)) {
            $user_id = $this->db->insert_id();

                        // NOTE THAT THE FOLLOWING 2 LINES ECHO THE CORRECT INFORMATION!!!!
            echo $user_id;
            echo $data['country'];

            // user profile data
            $user_profile_data['user_id'] = $user_id;
            $user_profile_data['country'] = $data['country'];

            if ($activated)    $this->create_profile($user_profile_data);

            return array('user_id' => $user_id);
        }
        return NULL;
    }

Code:
/**
     * Create an empty profile for a new user
     *
     * @param    int
     * @return    bool
     */
    private function create_profile($data)
    {

           // THEN WHEN IT COMES OUT HERE ...

        printf($data); // THIS RETURNS THE USER_ID???

        echo 'user_id: ' . $data['user_id']; // THIS RETURNS THE VALUE 1
        echo 'country: ' . $data['country']; // THIS RETURNS THE VALUE 1

          // WHY?

        $user_id = $data['user_id'];
        $country = $data['country'];

        $this->db->set('user_id', $user_id);
        $this->db->set('country', $country);

        return $this->db->insert(self::TABLE_PROFILE);
    }

Notice that in the create_user() function the variables echo correctly, yet when they're passed to the create_profile() function the user_id and country always return the values 1 and 1 respectively. The printf for the whole data array returns the correct value but only for the user_id. I don't really understand why the values aren't being passed correctly.

Can you give me some guidance here?
#67

[eluser]Gromozeka[/eluser]
[quote author="millo" date="1244122498"]Notice that in the create_user() function the variables echo correctly, yet when they're passed to the create_profile() function the user_id and country always return the values 1 and 1 respectively. The printf for the whole data array returns the correct value but only for the user_id. I don't really understand why the values aren't being passed correctly.

Can you give me some guidance here?[/quote]

Hi millo,

I suppose that I know where's error in your code. Please notice if-condition in the 'create_user' metod of 'user' model:

Code:
if ($activated)    $this->create_profile($user_profile_data);

If you set 'email_activation' flag in config-file to TRUE, then $activated value in the code above will be FALSE, and 'create_profile' method won't be called here. It will be called later, on user activation by email.

So if you want user profile to be created upon registration (not on activation) you have to get rid of this condition and comment 'create_profile' calling in 'activate_user' method of 'user' model:

Code:
function create_user($data, $activated = TRUE)
{
    ...

    $this->create_profile($user_profile_data);

    ...
}

Code:
function activate_user($user_id, $new_email_key)
{
    ...

//    $this->create_profile($user_id);

    ...
}

Hope that it solves your problem :)
#68

[eluser]millo[/eluser]
Thanks very much! Yes it does.

I guess I'd need to add a new function for editing the user_profile now. What's the best practice with that? Do I put it in the /models/tank_auth/users.php file or in the libraries/tank_auth.php library file? I'm just uncertain because I've noticed that you have the same function names in both of these files, like delete_user() for example.

Can you help me get a better understanding of why it's like this?
#69

[eluser]Gromozeka[/eluser]
Hi millo,

The library file is just an interface between controller and model. I consider the library structure as follows:
- model -- simple getters and setters, to retrieve and save data
- library -- process data from model or user (like validating user's password)
- controller -- control behaviour of the end-user interface

So if you just want to get data from user_profile table and show them to user, or save user input to database, there's no needs in extra-layer between model and controller. Just add another model file, put all required methods in it an call them directly from controller.
#70

[eluser]Zewt[/eluser]
Have a small question.

I have been testing out Tank Auth and am running into this db error.
Quote:A Database Error Occurred
Error Number: 1364

Field 'user_data' doesn't have a default value

INSERT INTO `ci_sessions` (`session_id`, `ip_address`, `user_agent`, `last_activity`) VALUES ('b3fc823b3c7fc718ef8a', '69.62.xxx.xx', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) Ap', 1244719880)

The
Code:
user_data
field is wanting some data obviously, as it is set to NOT NULL.
Code:
`user_data` text COLLATE utf8_bin NOT NULL,

Any ideas why I seem to be the only one having a problem with this?




Theme © iAndrew 2016 - Forum software by © MyBB