Skip to content

Commit

Permalink
User Guide finished. Added active, enabled/disabled, password_never_e…
Browse files Browse the repository at this point in the history
…xpires
  • Loading branch information
danmontgomery committed Jul 5, 2011
1 parent bc94b17 commit f6aea9b
Show file tree
Hide file tree
Showing 43 changed files with 3,506 additions and 208 deletions.
32 changes: 17 additions & 15 deletions application/config/bitauth.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Tables used by BitAuth
*/
$config['table'] = array(
'users' => 'bitauth_users',
'groups' => 'bitauth_groups',
'assoc' => 'bitauth_assoc'
);

/**
* Field users log in with, must be unique
* Users must be activated before they can login
*
*/
$config['username_field'] = 'username';
$config['require_user_activation'] = TRUE;

/**
* Default group users are added to when they first register
*/
$config['default_group'] = 'Users';
$config['default_group_id'] = 2;

/**
* Name of the cookie where "remember me" login is kept
Expand All @@ -39,7 +31,7 @@
/**
* Number of days before passwords expire. To disable, set to FALSE
*/
$config['pwd_max_age'] = FALSE;
$config['pwd_max_age'] = 90;

/**
* Number of days before password expiration to notify users their
Expand Down Expand Up @@ -80,6 +72,16 @@
'special' => '[[:punct:]]'
);

/**
* Tables used by BitAuth
*/
$config['table'] = array(
'users' => 'bitauth_users',
'data' => 'bitauth_userdata',
'groups' => 'bitauth_groups',
'assoc' => 'bitauth_assoc'
);

/**
* Your permissions slugs. These are how you call permissions checks
* in your code. eg: if($this->bitauth->has_perm('example_perm_1'))
Expand All @@ -88,7 +90,7 @@

/**
* THE FIRST PERMISSION IS ALWAYS THE ADMINISTRATOR PERMISSION
* ANY GROUPS GIVEN THIS PERMISSION WILL HAVE FULL ACCESS
* ANY USERS IN GROUPS GIVEN THIS PERMISSION WILL HAVE FULL ACCESS
*/
'is_admin' => 'User is an Administrator',

Expand All @@ -97,6 +99,6 @@
* Follow the format:
* 'permission_slug' => 'Permission Description',
*/
'can_edit' => 'Can Add/Edit Users & Groups(Sample)',
'can_edit' => 'Can Add/Edit Users & Groups (Sample)',
'can_change_pw' => 'Can Change User Passwords (Sample)'
);
211 changes: 191 additions & 20 deletions application/controllers/bitauth_example.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct()
*/
public function index()
{
if(! $this->bitauth->logged_in())
if( ! $this->bitauth->logged_in())
{
$this->session->set_userdata('redir', 'bitauth_example');
redirect('bitauth_example/login');
Expand All @@ -44,13 +44,13 @@ public function index()
*/
public function add_user()
{
if(! $this->bitauth->logged_in())
if( ! $this->bitauth->logged_in())
{
$this->session->set_userdata('redir', 'bitauth_example/add_user');
redirect('bitauth_example/login');
}

if(! $this->bitauth->has_perm('can_edit'))
if( ! $this->bitauth->has_perm('can_edit'))
{
$this->load->view('bitauth/no_access');
return;
Expand All @@ -64,14 +64,16 @@ public function add_user()
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('fullname', 'Fullname', '');
$this->form_validation->set_rules('password', 'Password', 'required|bitauth_valid_password');
$this->form_validation->set_rules('groups','Groups','');

if($this->form_validation->run() == TRUE)
{
$user = array(
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'fullname' => $this->input->post('fullname'),
'password' => $this->input->post('password')
'password' => $this->input->post('password'),
'groups' => $this->input->post('groups')
);

if($this->bitauth->add_user($user))
Expand Down Expand Up @@ -110,16 +112,18 @@ public function edit_user($user_id)
$this->form_validation->set_rules('username', 'Username', 'trim|required|bitauth_unique_username['.$user_id.']');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('fullname', 'Fullname', '');
$this->form_validation->set_rules('groups', 'Groups', '');

if($this->form_validation->run() == TRUE)
{
$user = array(
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'fullname' => $this->input->post('fullname')
'fullname' => $this->input->post('fullname'),
'groups' => $this->input->post('groups')
);

if($this->bitauth->update_user_info($user_id, $user))
if($this->bitauth->update_user($user_id, $user))
{
redirect('bitauth_example');
}
Expand All @@ -141,7 +145,7 @@ public function edit_user($user_id)
*/
public function groups()
{
if(! $this->bitauth->logged_in())
if( ! $this->bitauth->logged_in())
{
$this->session->set_userdata('redir', 'bitauth_example/groups');
redirect('bitauth_example/login');
Expand All @@ -160,13 +164,13 @@ public function add_group()

$this->output->enable_profiler();

if(! $this->bitauth->logged_in())
if( ! $this->bitauth->logged_in())
{
$this->session->set_userdata('redir', 'bitauth_example/add_group');
redirect('bitauth_example/login');
}

if(! $this->bitauth->has_perm('can_edit'))
if( ! $this->bitauth->has_perm('can_edit'))
{
$this->load->view('bitauth/no_access');
return;
Expand All @@ -182,18 +186,10 @@ public function add_group()

if($this->form_validation->run() == TRUE)
{
$permissions = gmp_init(0);
if($this->input->post('permissions'))
{
foreach($this->input->post('permissions') as $_perm => $on)
{
gmp_setbit($permissions, $_perm);
}
}
$group = array(
'name' => $this->input->post('name'),
'description' => $this->input->post('description'),
'permissions' => gmp_strval($permissions)
'permissions' => $this->input->post('permissions')
);

if($this->bitauth->add_group($group))
Expand All @@ -218,7 +214,7 @@ public function add_group()
*/
public function edit_group($group_id)
{
if(! $this->bitauth->logged_in())
if( ! $this->bitauth->logged_in())
{
$this->session->set_userdata('redir', 'bitauth_example/edit_group/'.$group_id);
redirect('bitauth_example/login');
Expand Down Expand Up @@ -261,9 +257,53 @@ public function edit_group($group_id)
* Bitauth_example::reset_password()
*
*/
public function reset_password()
public function reset_password($user_id = NULL)
{
if( ! $this->bitauth->logged_in())
{
$this->session->set_userdata('redir', 'bitauth_example');
redirect('bitauth_example/login');
}

$user = $this->bitauth->get_user_by_id($user_id);
if( ! $user )
{
redirect('bitauth_example');
}

if( ! $this->bitauth->has_perm('can_change_pw') || ( ! $this->bitauth->has_perm('is_admin') && $this->bitauth->has_perm('is_admin', $user->permissions)))
{
$this->load->view('bitauth/no_access');
return;
}

if($user_id === NULL)
{
$user_id = $this->bitauth->user_id;
}

$data = array('bitauth' => $this->bitauth);

if($this->input->post())
{
$this->form_validation->set_rules('password', 'Password', 'required|bitauth_valid_password');
$this->form_validation->set_rules('confirm_password', 'Confirm Password', 'required|matches[password]');

if($this->form_validation->run() == TRUE)
{
if($this->bitauth->set_password($user_id, $this->input->post('password')))
{
redirect('bitauth_example');
}

$data['error'] = $this->bitauth->get_error();
}
else
{
$data['error'] = validation_errors();
}
}

$this->load->view('bitauth/reset_password', $data);
}

Expand Down Expand Up @@ -308,6 +348,25 @@ public function register()
$this->load->view('bitauth/register', $data);
}

/**
* Bitauth_example::activate()
*
*/
public function activate($activation_code)
{
if($this->bitauth->activate($activation_code))
{
if($this->bitauth->logged_in())
{
redirect('bitauth_example');
}

redirect('bitauth_example/login');
}

echo lang('bitauth_code_not_found');
}

/**
* Bitauth_example::login()
*
Expand All @@ -320,6 +379,7 @@ public function login()
{
$this->form_validation->set_rules('username', 'Username', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('remember_me','Remember Me','');

if($this->form_validation->run() == TRUE)
{
Expand Down Expand Up @@ -358,4 +418,115 @@ public function logout()
redirect('bitauth_example');
}

public function test()
{
$user = array('username' => 'testuser123','password' => 'testuser123','fullname' => 'Test User 123');
$group = array('name' => 'Test Group 123');

echo '<table width="50%">';

echo '<tr><td width="50%">Add User</td>';
if($this->bitauth->add_user($user))
echo '<td style="font-color: green;">Success</td>';
else
echo '<td style="font-color: red;">Failed: '.$this->bitauth->get_error().'</td>';
echo '</tr>';

echo '<tr><td width="50%">Add Group</td>';
if($this->bitauth->add_group($group))
echo '<td style="font-color: green;">Success</td>';
else
echo '<td style="font-color: red;">Failed: '.$this->bitauth->get_error().'</td>';
echo '</tr>';

echo '<tr><td width="50%">Fetch User</td>';
if($_user = $this->bitauth->get_user_by_username($user['username']))
echo '<td style="font-color: green;">Success</td>';
else
echo '<td style="font-color: red;">Failed: '.$this->bitauth->get_error().'</td>';
echo '</tr>';

echo '<tr><td width="50%">Fetch Group</td>';
if($_group = $this->bitauth->get_group_by_name($group['name']))
echo '<td style="font-color: green;">Success</td>';
else
echo '<td style="font-color: red;">Failed: '.$this->bitauth->get_error().'</td>';
echo '</tr>';

echo '<tr><td width="50%">Activate User</td>';
if($this->bitauth->activate($_user->activation_code))
echo '<td style="font-color: green;">Success</td>';
else
echo '<td style="font-color: red;">Failed: '.$this->bitauth->get_error().'</td>';
echo '</tr>';

echo '<tr><td width="50%">Edit User (Empty Groups)</td>';
$_user = $this->bitauth->get_user_by_username($user['username']);
if($this->bitauth->update_user($_user->user_id, array('groups' => '')))
echo '<td style="font-color: green;">Success</td>';
else
echo '<td style="font-color: red;">Failed: '.$this->bitauth->get_error().'</td>';
echo '</tr>';

echo '<tr><td width="50%">Edit Group (Add User)</td>';
if($this->bitauth->update_group($_group->group_id, array('members' => array($_user->user_id))))
echo '<td style="font-color: green;">Success</td>';
else
echo '<td style="font-color: red;">Failed: '.$this->bitauth->get_error().'</td>';
echo '</tr>';

echo '<tr><td width="50%">Disable User</td>';
$_user = $this->bitauth->get_user_by_username($user['username']);
if($this->bitauth->disable($_user->user_id))
echo '<td style="font-color: green;">Success</td>';
else
echo '<td style="font-color: red;">Failed: '.$this->bitauth->get_error().'</td>';
echo '</tr>';
$this->bitauth->enable($_user->user_id);

echo '<tr><td width="50%">Password Almost Expired</td>';
$_user = $this->bitauth->get_user_by_username($user['username']);
$_user->password_last_set = date('Y-m-d H:i:s', strtotime('88 days ago'));
$this->bitauth->update_user($_user->user_id, $_user);
if($this->bitauth->password_almost_expired($_user->user_id))
echo '<td style="font-color: green;">Yes</td>';
else
echo '<td style="font-color: red;">No</td>';
echo '</tr>';

echo '<tr><td width="50%">Password Is Expired</td>';
$_user = $this->bitauth->get_user_by_username($user['username']);
$_user->password_last_set = date('Y-m-d H:i:s', strtotime('1 year ago'));
$this->bitauth->update_user($_user->user_id, $_user);
if($this->bitauth->password_is_expired($_user->user_id))
echo '<td style="font-color: green;">Yes</td>';
else
echo '<td style="font-color: red;">No</td>';
echo '</tr>';

echo '<tr><td width="50%">Reset Password</td>';
if($this->bitauth->set_password($_user->user_id, 'derp'))
echo '<td style="font-color: green;">Success</td>';
else
echo '<td style="font-color: red;">Failed: '.$this->bitauth->get_error().'</td>';
echo '</tr>';

echo '<tr><td width="50%">Delete Group</td>';
if($this->bitauth->delete_group($_group->group_id))
echo '<td style="font-color: green;">Success</td>';
else
echo '<td style="font-color: red;">Failed: '.$this->bitauth->get_error().'</td>';
echo '</tr>';

echo '<tr><td width="50%">Delete User</td>';
if($this->bitauth->delete($_user->user_id))
echo '<td style="font-color: green;">Success</td>';
else
echo '<td style="font-color: red;">Failed: '.$this->bitauth->get_error().'</td>';
echo '</tr>';

echo '</table>';

}

}
Loading

0 comments on commit f6aea9b

Please sign in to comment.