Skip to content

Commit

Permalink
Add a password strength meter.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Almdal committed Oct 27, 2009
1 parent 156a99b commit 1347a30
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 11 deletions.
13 changes: 8 additions & 5 deletions modules/user/controllers/admin_users.php
Expand Up @@ -63,7 +63,9 @@ public function add_user() {
}

public function add_user_form() {
print $this->_get_user_add_form_admin();
$v = new View("user_form.html");
$v->form = $this->_get_user_add_form_admin();
print $v;
}

public function delete_user($id) {
Expand Down Expand Up @@ -156,12 +158,13 @@ public function edit_user_form($id) {
kohana::show_404();
}

$form = $this->_get_user_edit_form_admin($user);
$v = new View("user_form.html");
$v->form = $this->_get_user_edit_form_admin($user);
// Don't allow the user to control their own admin bit, else you can lock yourself out
if ($user->id == identity::active_user()->id) {
$form->edit_user->admin->disabled(1);
$v->form->edit_user->admin->disabled(1);
}
print $form;
print $v;
}

public function add_user_to_group($user_id, $group_id) {
Expand Down Expand Up @@ -330,7 +333,7 @@ static function _get_user_add_form_admin() {
$form->add_rules_from(ORM::factory("user"));

$minimum_length = module::get_var("user", "mininum_password_length", 5);
$form->edit_user->password
$form->add_user->password
->rules($minimum_length ? "length[$minimum_length, 40]" : "length[40]");

module::event("user_add_form_admin", $user, $form);
Expand Down
11 changes: 6 additions & 5 deletions modules/user/controllers/password.php
Expand Up @@ -32,7 +32,7 @@ public function do_reset() {
if (request::method() == "post") {
$this->_change_password();
} else {
$user = user::lookup_user_by_field("hash", Input::instance()->get("key"));
$user = user::lookup_by_hash(Input::instance()->get("key"));
if (!empty($user)) {
print $this->_new_password_form($user->hash);
} else {
Expand All @@ -46,7 +46,7 @@ private function _send_reset() {

$valid = $form->validate();
if ($valid) {
$user = identity::lookup_user_by_name($form->reset->inputs["name"]->value);
$user = user::lookup_by_name($form->reset->inputs["name"]->value);
if (!$user->loaded || empty($user->email)) {
$form->reset->inputs["name"]->add_error("no_email", 1);
$valid = false;
Expand Down Expand Up @@ -110,19 +110,20 @@ private function _new_password_form($hash=null) {
"mistyped", t("The password and the confirm password must match"));
$group->submit("")->value(t("Update"));

$template->content = $form;
$template->content = new View("user_form.html");
$template->content->form = $form;
return $template;
}

private function _change_password() {
$view = $this->_new_password_form();
if ($view->content->validate()) {
if ($view->content->form->validate()) {
$user = user::lookup_by_hash(Input::instance()->post("hash"));
if (empty($user)) {
throw new Exception("@todo FORBIDDEN", 503);
}

$user->password = $view->content->reset->password->value;
$user->password = $view->content->form->reset->password->value;
$user->hash = null;
$user->save();
message::success(t("Password reset successfully"));
Expand Down
4 changes: 3 additions & 1 deletion modules/user/controllers/users.php
Expand Up @@ -63,7 +63,9 @@ public function form_edit($id) {
access::forbidden();
}

print $this->_get_edit_form($user);
$v = new View("user_form.html");
$v->form = $this->_get_edit_form($user);
print $v;
}

private function _get_edit_form($user) {
Expand Down
Binary file added modules/user/css/progressImg1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions modules/user/css/user.css
Expand Up @@ -54,3 +54,39 @@ li.g-group .g-user .g-button {
li.g-default-group h4, li.g-default-group .g-user {
color: gray;
}

.g-password-strength0 {
background: url(progressImg1.png) no-repeat 0 0;
width: 138px;
height: 7px;
}
.g-password-strength10 {
background-position:0 -7px;
}
.g-password-strength20 {
background-position:0 -14px;
}
.g-password-strength30 {
background-position:0 -21px;
}
.g-password-strength40 {
background-position:0 -28px;
}
.g-password-strength50 {
background-position:0 -35px;
}
.g-password-strength60 {
background-position:0 -42px;
}
.g-password-strength70 {
background-position:0 -49px;
}
.g-password-strength80 {
background-position:0 -56px;
}
.g-password-strength90 {
background-position:0 -63px;
}
.g-password-strength100 {
background-position:0 -70px;
}
2 changes: 2 additions & 0 deletions modules/user/helpers/user_theme.php
Expand Up @@ -20,9 +20,11 @@
class user_theme_Core {
static function head($theme) {
$theme->css("user.css");
$theme->script("password_strength.js");
}

static function admin_head($theme) {
$theme->css("user.css");
$theme->script("password_strength.js");
}
}
39 changes: 39 additions & 0 deletions modules/user/js/password_strength.js
@@ -0,0 +1,39 @@
(function($) {
// Based on the Password Strength Indictor By Benjamin Sterling
// http://benjaminsterling.com/password-strength-indicator-and-generator/
$.widget("ui.user_password_strength", {
_init: function() {
var self = this;
$(this.element).keyup(function() {
var strength = self.calculateStrength (this.value);
var index = Math.min(Math.floor( strength / 10 ), 10);
$("#g-password-gauge")
.removeAttr('class')
.addClass( "g-password-strength0" )
.addClass( self.options.classes[ index ] );
}).after("<div id='g-password-gauge' class='g-password-strength0'></div>");
},

calculateStrength: function(value) {
// Factor in the length of the password
var strength = Math.min(5, value.length) * 10 - 20;
// Factor in the number of numbers
strength += Math.min(3, value.length - value.replace(/[0-9]/g,"").length) * 10;
// Factor in the number of non word characters
strength += Math.min(3, value.length - value.replace(/\W/g,"").length) * 15;
// Factor in the number of Upper case letters
strength += Math.min(3, value.length - value.replace(/[A-Z]/g,"").length) * 10;

// Normalizxe between 0 and 100
return Math.max(0, Math.min(100, strength));
}
});
$.extend($.ui.user_password_strength, {
defaults: {
classes : ['g-password-strength10', 'g-password-strength20', 'g-password-strength30',
'g-password-strength40', 'g-password-strength50', 'g-password-strength60',
'g-password-strength70',' g-password-strength80',' g-password-strength90',
'g-password-strength100']
}
});
})(jQuery);
7 changes: 7 additions & 0 deletions modules/user/views/user_form.html.php
@@ -0,0 +1,7 @@
<?php defined("SYSPATH") or die("No direct script access.") ?>
<script type="text/javascript">
$("form").ready(function(){
$('input[name="password"]').user_password_strength();
});
</script>
<?= $form ?>

0 comments on commit 1347a30

Please sign in to comment.