• No results found

role_uri table

In document DX Auth (Page 34-52)

Obsolete in 1.0.2 above. Use permissions table.

Troubleshooting

DX Auth library might failed sending email if you didn't set the email setting well.

If that's happened, you need to create email.php in application/config/ folder, and paste following code. Edit it to fit your needs.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config['protocol'] = 'smtp';

$config['smtp_host'] = 'mail.localhost.com'; $config['smtp_user'] = 'username';

$config['smtp_pass'] = 'password'; $config['smtp_port'] = '25';

For more information about this, you can consult Code Igniter email helper.

Simple example

Before trying the example make sure you have follow installation instruction first.

Using DX Auth library it's pretty straight forward and simple, for example let's create a class named Auth in Auth controller.

view plaincopy to clipboardprint?

1.

class Auth extends Controller 2. {

3.

function Auth() 4. { 5. parent::Controller();

6.

// Load library

7.

$this->load->library('DX_Auth'); 8. }

9.

10.

function login() 11. {

12.

// Login using username 'test' and password 'helloworld'

13.

$this->dx_auth->login('test', 'helloworld'); 14. } 15.

16.

function logout() 17. {

18.

// Logout user

19.

$this->dx_auth->logout(); 20. } 21.

22.

function register() 23. {

24.

// Register a user with username 'john', password 'johnpassword', and email 'john@yourm ail.com'

25.

if ($user = $this->dx_auth->register('john', 'johnpassword', '[email protected]')) 26. {

27.

echo 'Welcome '.$user->username; 28. }

29.

else 30. {

31.

echo 'Failed to register'; 32. }

33. } 34.

35.

function hello() 36. {

37.

// Check if user is logged in or not

38.

if ($this->dx_auth->is_logged_in()) 39. {

40.

echo 'Hello world'; 41. }

44.

echo 'Not logged in'; 45. }

46. } 47.}

By just looking these example, i think you already get a grip how easy and simple to use DX Auth library.

If you are interested, here is the more advanced example.

Advanced example

This is more advanced, and how DX Auth should be implemented. You can see explanation commented in source code.

view plaincopy to clipboardprint?

1.

class Auth extends Controller 2. {

3.

// Used for registering and changing password form validation

4.

var $min_username = 4;

5.

var $max_username = 20;

6.

var $min_password = 4;

7.

var $max_password = 20; 8.

9.

function Auth() 10. { 11. parent::Controller(); 12.

13.

$this->load->library('Form_validation');

14.

$this->load->library('DX_Auth'); 15.

16.

$this->load->helper('url');

17.

$this->load->helper('form'); 18. } 19.

20.

function index() 21. {

22.

$this->login(); 23. } 24.

25.

/* Callback function */ 26.

27.

function username_check($username) 28. {

29.

$result = $this->dx_auth->is_username_available($username);

30.

if ( ! $result) 31. {

32.

$this->form_validation->set_message('username_check', 'Username already exist. Plea se choose another username.');

33. } 34.

35.

return $result; 36. } 37.

38.

function email_check($email) 39. {

40.

$result = $this->dx_auth->is_email_available($email);

41.

if ( ! $result) 42. {

43.

$this->form_validation->set_message('email_check', 'Email is already used by another user. Please choose another email address.');

44. } 45.

46.

return $result; 47. } 48.

49.

function captcha_check($code) 50. {

51.

$result = TRUE; 52.

53.

if ($this->dx_auth->is_captcha_expired()) 54. {

55.

// Will replace this error msg with $lang

56.

$this->form_validation->set_message('captcha_check', 'Your confirmation code has ex pired. Please try again.');

59.

elseif ( ! $this->dx_auth->is_captcha_match($code)) 60. {

61.

$this->form_validation->set_message('captcha_check', 'Your confirmation code does n ot match the one in the image. Try again.');

62.

$result = FALSE; 63. } 64.

65.

return $result; 66. } 67.

68.

/* End of Callback function */ 69.

70.

function login() 71. {

72.

if ( ! $this->dx_auth->is_logged_in()) 73. {

74.

$val = $this->form_validation; 75.

76.

// Set form validation rules

77.

$val->set_rules('username', 'Username', 'trim|required|xss_clean');

78.

$val->set_rules('password', 'Password', 'trim|required|xss_clean');

79.

$val->set_rules('remember', 'Remember me', 'integer'); 80.

81.

// Set captcha rules if login attempts exceed max attempts in config

82.

if ($this->dx_auth->is_max_login_attempts_exceeded()) 83. {

84.

$val->set_rules('captcha', 'Confirmation Code', 'trim|required|xss_clean| callback_captcha_check');

85. } 86.

87.

if ($val->run() AND $this->dx_auth->login($val->set_value('username'), $val- >set_value('password'), $val->set_value('remember'))) 88. {

89.

// Redirect to homepage

90.

redirect('', 'location'); 91. }

92.

else

93. {

94.

// Check if the user is failed logged in because user is banned user or not

95.

if ($this->dx_auth->is_banned()) 96. {

97.

// Redirect to banned uri

98.

$this->dx_auth->deny_access('banned'); 99. }

100.

else

101. {

102.

// Default is we don't show captcha until max login attempts eceeded

103.

$data['show_captcha'] = FALSE; 104.

105.

// Show captcha if login attempts exceed max attempts in config

106.

if ($this->dx_auth->is_max_login_attempts_exceeded()) 107. {

108.

// Create catpcha

109.

$this->dx_auth->captcha(); 110.

111.

// Set view data to show captcha on view file

112.

$data['show_captcha'] = TRUE; 113. }

114.

115.

// Load login page view

116.

$this->load->view($this->dx_auth->login_view, $data); 117. } 118. } 119. }

120.

else 121. {

122.

$data['auth_message'] = 'You are already logged in.';

123.

$this->load->view($this->dx_auth->logged_in_view, $data); 124. }

125. } 126.

129.

$this->dx_auth->logout(); 130.

131.

$data['auth_message'] = 'You have been logged out.';

132.

$this->load->view($this->dx_auth->logout_view, $data); 133. }

134.

135.

function register() 136. {

137.

if ( ! $this->dx_auth->is_logged_in() AND $this->dx_auth->allow_registration) 138. {

139.

$val = $this->form_validation; 140.

141.

// Set form validation rules

142.

$val->set_rules('username', 'Username', 'trim|required|xss_clean|min_length['. $this->min_username.']|max_length['.$this->max_username.']|callback_username_check| alpha_dash');

143.

$val->set_rules('password', 'Password', 'trim|required|xss_clean|min_length['. $this->min_password.']|max_length['.$this->max_password.']|matches[confirm_password]');

144.

$val->set_rules('confirm_password', 'Confirm Password', 'trim|required| xss_clean');

145.

$val->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email| callback_email_check');

146.

147.

if ($this->dx_auth->captcha_registration) 148. {

149.

$val->set_rules('captcha', 'Confirmation Code', 'trim|xss_clean|required| callback_captcha_check');

150. }

151.

152.

// Run form validation and register user if it's pass the validation

153.

if ($val->run() AND $this->dx_auth->register($val->set_value('username'), $va l->set_value('password'), $val->set_value('email')))

154. {

155.

// Set success message accordingly

156.

if ($this->dx_auth->email_activation) 157. {

158.

$data['auth_message'] = 'You have successfully registered. Check your em ail address to activate your account.';

159. }

160.

else

161. {

162.

$data['auth_message'] = 'You have successfully registered. '.anchor(site_u rl($this->dx_auth->login_uri), 'Login');

163. } 164.

165.

// Load registration success page

166.

$this->load->view($this->dx_auth->register_success_view, $data); 167. }

168.

else 169. {

170.

// Is registration using captcha

171.

if ($this->dx_auth->captcha_registration) 172. {

173.

$this->dx_auth->captcha(); 174. }

175.

176.

// Load registration page

177.

$this->load->view($this->dx_auth->register_view); 178. }

179. }

180.

elseif ( ! $this->dx_auth->allow_registration) 181. {

182.

$data['auth_message'] = 'Registration has been disabled.';

183.

$this->load->view($this->dx_auth->register_disabled_view, $data); 184. }

185.

else 186. {

187.

$data['auth_message'] = 'You have to logout first, before registering.';

188.

$this->load->view($this->dx_auth->logged_in_view, $data); 189. }

190. } 191.

194.

// Get username and key

195.

$username = $this->uri->segment(3);

196.

$key = $this->uri->segment(4); 197.

198.

// Activate user

199.

if ($this->dx_auth->activate($username, $key)) 200. {

201.

$data['auth_message'] = 'Your account have been successfully activated. '.ancho r(site_url($this->dx_auth->login_uri), 'Login');

202.

$this->load->view($this->dx_auth->activate_success_view, $data); 203. }

204.

else 205. {

206.

$data['auth_message'] = 'The activation code you entered was incorrect. Please check your email again.';

207.

$this->load->view($this->dx_auth->activate_failed_view, $data); 208. } 209. } 210.

211.

function forgot_password() 212. {

213.

$val = $this->form_validation; 214.

215.

// Set form validation rules

216.

$val->set_rules('login', 'Username or Email address', 'trim|required|xss_clean');

217.

218.

// Validate rules and call forgot password function

219.

if ($val->run() AND $this->dx_auth->forgot_password($val->set_value('login'))) 220. {

221.

$data['auth_message'] = 'An email has been sent to your email with instructions with how to activate your new password.';

222.

$this->load->view($this->dx_auth->forgot_password_success_view, $data); 223. }

224.

else 225. {

227. } 228. } 229.

230.

function reset_password() 231. {

232.

// Get username and key

233.

$username = $this->uri->segment(3);

234.

$key = $this->uri->segment(4); 235.

236.

// Reset password

237.

if ($this->dx_auth->reset_password($username, $key)) 238. {

239.

$data['auth_message'] = 'You have successfully reset you password, '.anchor(sit e_url($this->dx_auth->login_uri), 'Login');

240.

$this->load->view($this->dx_auth->reset_password_success_view, $data); 241. }

242.

else 243. {

244.

$data['auth_message'] = 'Reset failed. Your username and key are incorrect. Ple ase check your email again and follow the instructions.';

245.

$this->load->view($this->dx_auth->reset_password_failed_view, $data); 246. } 247. } 248.

249.

function change_password() 250. {

251.

// Check if user logged in or not

252.

if ($this->dx_auth->is_logged_in()) 253. {

254.

$val = $this->form_validation; 255.

256.

// Set form validation

257.

$val->set_rules('old_password', 'Old Password', 'trim|required|xss_clean| min_length['.$this->min_password.']|max_length['.$this->max_password.']');

258.

$val->set_rules('new_password', 'New Password', 'trim|required|xss_clean| min_length['.$this->min_password.']|max_length['.$this->max_password.']|

259.

$val->set_rules('confirm_new_password', 'Confirm new Password', 'trim| required|xss_clean');

260.

261.

// Validate rules and change password

262.

if ($val->run() AND $this->dx_auth->change_password($val- >set_value('old_password'), $val->set_value('new_password')))

263. {

264.

$data['auth_message'] = 'Your password has successfully been changed.';

265.

$this->load->view($this->dx_auth->change_password_success_view, $dat a); 266. }

267.

else 268. {

269.

$this->load->view($this->dx_auth->change_password_view); 270. } 271. }

272.

else 273. {

274.

// Redirect to login page

275.

$this->dx_auth->deny_access('login'); 276. } 277. } 278.

279.

function cancel_account() 280. {

281.

// Check if user logged in or not

282.

if ($this->dx_auth->is_logged_in()) 283. {

284.

$val = $this->form_validation; 285.

286.

// Set form validation rules

287.

$val->set_rules('password', 'Password', "trim|required|xss_clean"); 288.

289.

// Validate rules and change password

290.

if ($val->run() AND $this->dx_auth->cancel_account($val- >set_value('password')))

292.

// Redirect to homepage

293.

redirect('', 'location'); 294. }

295.

else 296. {

297.

$this->load->view($this->dx_auth->cancel_account_view); 298. } 299. }

300.

else 301. {

302.

// Redirect to login page

303.

$this->dx_auth->deny_access('login'); 304. }

305. }

306. }

You can find this example in controllers/auth.php that included in DX Auth library download.

Recatpcha example

This is an advanced example how to use reCAPTCHA in registration. Make sure you already insert reCAPTCHA key in config file, if not the example wouldn't work.

Here is the controller part.

view plaincopy to clipboardprint?

1.

class Auth extends Controller 2. {

3.

// Used for registering and changing password form validation

4.

var $min_username = 4;

5.

var $max_username = 20;

6.

var $min_password = 6;

7.

var $max_password = 10; 8.

9.

function Auth() 10. { 11. parent::Controller(); 12.

14.

$this->load->library('DX_auth'); 15. } 16.

17.

function index() 18. {

19.

$this->login(); 20. } 21.

22.

/* Callback function */ 23.

24.

function username_check($username) 25. {

26.

$result = $this->dx_auth->is_username_available($username);

27.

if ( ! $result) 28. {

29.

$this->form_validation->set_message('username_check', 'Username already exist. Plea se choose another username.');

30. } 31.

32.

return $result; 33. } 34.

35.

function email_check($email) 36. {

37.

$result = $this->dx_auth->is_email_available($email);

38.

if ( ! $result) 39. {

40.

$this->form_validation->set_message('email_check', 'Email is already used by another user. Please choose another email address.');

41. } 42.

43.

return $result; 44. } 45.

46.

function recaptcha_check() 47. {

48.

$result = $this->dx_auth->is_recaptcha_match();

49.

if ( ! $result) 50. {

51.

$this->form_validation->set_message('recaptcha_check', 'Your confirmation code does not match the one in the image. Try again.');

52. } 53.

54.

return $result; 55. }

56.

57.

/* End of Callback function */ 58.

59.

function register_recaptcha() 60. {

61.

if ( ! $this->dx_auth->is_logged_in() AND $this->dx_auth->allow_registration) 62. {

63.

$val = $this->form_validation; 64.

65.

// Set form validation rules

66.

$val->set_rules('username', 'Username', 'trim|required|xss_clean|min_length['.$this- >min_username.']|max_length['.$this->max_username.']|callback_username_check|

alpha_dash');

67.

$val->set_rules('password', 'Password', 'trim|required|xss_clean|min_length['.$this- >min_password.']|max_length['.$this->max_password.']|matches[confirm_password]');

68.

$val->set_rules('confirm_password', 'Confirm Password', 'trim|required|xss_clean');

69.

$val->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email| callback_email_check');

70.

71.

// Is registration using captcha

72.

if ($this->dx_auth->captcha_registration) 73. {

74.

// Set recaptcha rules.

75.

// IMPORTANT: Do not change 'recaptcha_response_field' because it's used by reCAP TCHA API,

76.

// This is because the limitation of reCAPTCHA, not DX Auth library

77.

$val->set_rules('recaptcha_response_field', 'Confirmation Code', 'trim|xss_clean| required|callback_recaptcha_check');

79.

80.

// Run form validation and register user if it's pass the validation

81.

if ($val->run() AND $this->dx_auth->register($val->set_value('username'), $val- >set_value('password'), $val->set_value('email')))

82. {

83.

// Set success message accordingly

84.

if ($this->dx_auth->email_activation) 85. {

86.

$data['auth_message'] = 'You have successfully registered. Check your email add ress to activate your account.';

87. }

88.

else

89. {

90.

$data['auth_message'] = 'You have successfully registered. '.anchor(site_url($this ->dx_auth->login_uri), 'Login');

91. } 92.

93.

// Load registration success page

94.

$this->load->view($this->dx_auth->register_success_view, $data); 95. }

96.

else 97. {

98.

// Load registration page

99.

$this->load->view('auth/register_recaptcha_form'); 100. }

101. }

102.

elseif ( ! $this->dx_auth->allow_registration) 103. {

104.

$data['auth_message'] = 'Registration has been disabled.';

105.

$this->load->view($this->dx_auth->register_disabled_view, $data); 106. }

107.

else 108. {

109.

$data['auth_message'] = 'You have to logout first, before registering.';

110.

$this->load->view($this->dx_auth->logged_in_view, $data); 111. }

113. }

Here is the view part (auth/register_recaptcha_form).

view plaincopy to clipboardprint?

1. <?php

2.

$username = array(

3.

'name' => 'username',

4.

'id' => 'username',

5.

'size' => 30,

6.

'value' => set_value('username') 7. ); 8.

9.

$password = array(

10.

'name' => 'password',

11.

'id' => 'password',

12.

'size' => 30,

13.

'value' => set_value('password') 14.); 15.

16.

$confirm_password = array(

17.

'name' => 'confirm_password',

18.

'id' => 'confirm_password',

19.

'size' => 30,

20.

'value' => set_value('confirm_password') 21.); 22.

23.

$email = array(

24.

'name' => 'email',

25.

'id' => 'email',

26.

'maxlength' => 80,

27.

'size' => 30,

28.

'value' => set_value('email') 29.); 30.?> 31.

33.<body> 34. 35.<fieldset><legend>Register</legend>

36.

<?php echo form_open($this->uri->uri_string())?> 37. 38.<dl>

39.

<dt><?php echo form_label('Username', $username['id']);?></dt> 40. <dd>

41.

<?php echo form_input($username)?>

42.

<?php echo form_error($username['name']); ?> 43. 44. </dd> 45.

46.

<dt><?php echo form_label('Password', $password['id']);?></dt> 47. <dd>

48.

<?php echo form_password($password)?>

49.

<?php echo form_error($password['name']); ?> 50. 51. </dd> 52.

53.

<dt><?php echo form_label('Confirm Password', $confirm_password['id']);?></dt> 54. <dd>

55.

<?php echo form_password($confirm_password);?>

56.

<?php echo form_error($confirm_password['name']); ?> 57. 58. </dd> 59.

60.

<dt><?php echo form_label('Email Address', $email['id']);?></dt> 61. <dd>

62.

<?php echo form_input($email);?>

63.

<?php echo form_error($email['name']); ?> 64. 65. </dd> 66.

67.

<?php if ($this->dx_auth->captcha_registration): ?> 68.

69. <dt></dt> 70. <dd> 71. <?php

72.

// Show recaptcha imgage

73.

echo $this->dx_auth->get_recaptcha_image();

74.

// Show reload captcha link

75.

echo $this->dx_auth->get_recaptcha_reload_link();

76.

// Show switch to image captcha or audio link

77.

echo $this->dx_auth->get_recaptcha_switch_image_audio_link(); 78. ?> 79. 80. </dd> 81.

82.

<dt><?php echo $this->dx_auth->get_recaptcha_label(); ?></dt> 83. <dd>

84.

<?php echo $this->dx_auth->get_recaptcha_input(); ?> 85.

86.

<?php echo form_error('recaptcha_response_field'); ?> 87. </dd> 88. 89. <?php

90.

// Get recaptcha javascript and non javasript html

91.

echo $this->dx_auth->get_recaptcha_html(); 92. ?>

93.

<?php endif; ?> 94. 95. 96. 97. <dt></dt> 98.

99.

<dd><?php echo form_submit('register','Register');?></dd> 100. </dl> 101.

102.

<?php echo form_close()?> 103. </fieldset>

105. </html>

You can find this example in controllers/auth.php and

views/auth/register_recaptcha_form.php that included in DX Auth library download.

Top of Page

In document DX Auth (Page 34-52)

Related documents