summaryrefslogtreecommitdiff
path: root/modules/user/models
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2010-01-16 21:15:12 -0800
committerBharat Mediratta <bharat@menalto.com>2010-01-16 21:15:12 -0800
commit6a4dda9bdef81bcf79abe5601fd7309e593078f3 (patch)
tree6b6dbe69481ec3f1bbb783b7ce5c843c03090180 /modules/user/models
parenta691dcc63cb6403784e061997cc85606a8f953b3 (diff)
Convert Admin_Users_Controller, User_Model and Group_Model to use
model based validation.
Diffstat (limited to 'modules/user/models')
-rw-r--r--modules/user/models/group.php39
-rw-r--r--modules/user/models/user.php18
2 files changed, 46 insertions, 11 deletions
diff --git a/modules/user/models/group.php b/modules/user/models/group.php
index 10f6f4b3..16d6adb7 100644
--- a/modules/user/models/group.php
+++ b/modules/user/models/group.php
@@ -20,8 +20,7 @@
class Group_Model extends ORM implements Group_Definition {
protected $has_and_belongs_to_many = array("users");
- var $form_rules = array(
- "name" => "required|length[4,255]");
+ var $rules = array("name" => array("rules" => array("required", "length[4,255]")));
/**
* @see ORM::delete()
@@ -37,18 +36,42 @@ class Group_Model extends ORM implements Group_Definition {
return $this->users->find_all();
}
- public function save() {
- if (!$this->loaded()) {
- $created = 1;
+ /**
+ * Add some custom per-instance rules.
+ */
+ public function validate($array=null) {
+ // validate() is recursive, only modify the rules on the outermost call.
+ if (!$array) {
+ $this->rules["name"]["callbacks"] = array(array($this, "valid_name"));
}
- $original = clone $this->original();
- parent::save();
- if (isset($created)) {
+ parent::validate($array);
+ }
+
+ public function save() {
+ if (!$this->loaded()) {
+ // New group
+ parent::save();
module::event("group_created", $this);
} else {
+ // Updated group
+ $original = clone $this->original();
+ parent::save();
module::event("group_updated", $original, $this);
}
+
return $this;
}
+
+ /**
+ * Validate the user name. Make sure there are no conflicts.
+ */
+ public function valid_name(Validation $v, $field) {
+ if (db::build()->from("groups")
+ ->where("name", "=", $this->name)
+ ->where("id", "<>", $this->id)
+ ->count_records() == 1) {
+ $v->add_error("name", "in_use");
+ }
+ }
} \ No newline at end of file
diff --git a/modules/user/models/user.php b/modules/user/models/user.php
index 12da5784..c45f88ac 100644
--- a/modules/user/models/user.php
+++ b/modules/user/models/user.php
@@ -78,6 +78,7 @@ class User_Model extends ORM implements User_Definition {
}
$this->rules["password"]["callbacks"] = array(array($this, "valid_password"));
+ $this->rules["admin"]["callbacks"] = array(array($this, "valid_admin"));
parent::validate($array);
}
@@ -131,9 +132,20 @@ class User_Model extends ORM implements User_Definition {
* Validate the password.
*/
public function valid_password(Validation $v, $field) {
- $minimum_length = module::get_var("user", "mininum_password_length", 5);
- if ($this->password_length < $minimum_length || $this->password_length > 40) {
- $v->add_error("password", "length");
+ if (!$this->loaded() || $this->password_length) {
+ $minimum_length = module::get_var("user", "mininum_password_length", 5);
+ if ($this->password_length < $minimum_length || $this->password_length > 40) {
+ $v->add_error("password", "length");
+ }
+ }
+ }
+
+ /**
+ * Validate the admin bit.
+ */
+ public function valid_admin(Validation $v, $field) {
+ if ($this->id == identity::active_user()->id && !$this->admin) {
+ $v->add_error("admin", "locked");
}
}
}