Permalink
Browse files

assigning roles for users (incomplete)

  • Loading branch information...
1 parent ab92e1b commit 87bb0793092f1121a8029b10faae9880b695c840 Amos Chan committed May 12, 2012
Showing with 61 additions and 6 deletions.
  1. +25 −0 Controller/UsersController.php
  2. +10 −2 Model/User.php
  3. +22 −0 View/Users/assign_roles.ctp
  4. +2 −2 View/Users/edit.ctp
  5. +2 −2 View/Users/index.ctp
@@ -6,6 +6,7 @@ class UsersController extends UrgAppController {
var $modelName;
var $components = array("Urg.Urg");
+ var $helpers = array("TwitterBootstrap.TwitterBootstrap", "Form", "Session", "Html");
function index() {
$this->User->recursive = 0;
@@ -73,6 +74,30 @@ function __get_roles_list() {
return $roles;
}
+ function assign_roles($username = null) {
+ $habtms = array("Role" => array("className" => "Urg.Role"));
+ $this->User->bindModel(array("hasAndBelongsToMany" => $habtms));
+ if ($this->request->is('post') || $this->request->is('put')) {
+ $user = $this->User->findById($this->request->data["User"]["id"]);
+ $user["User"]["password"] = null;
+ //var_dump($this->request->data);
+ if ($this->User->saveAll($this->request->data)) {
+ $this->Session->setFlash(__("Roles successfully assigned"));
+ $this->redirect(array("action" => "index"));
+ } else {
+ $this->Session->setFlash(__("Couldn't assign roles"));
+ }
+ }
+ if ($username != null) {
+ $user = $this->User->findByUsername($username);
+ $this->request->data = $user;
+ }
+
+ $roles = $this->__get_roles_list();
+ $this->set("user", $user);
+ $this->set("roles", $this->User->Role->find("list"));
+ }
+
function edit($id = null) {
if (!$id && empty($this->request->data)) {
$this->Session->setFlash(__('Invalid user'));
View
@@ -9,7 +9,8 @@ class User extends UrgAppModel {
"password" => array(
"notEmpty" => array(
"rule" => "notEmpty",
- "message" => "errors.users.password.required"
+ "message" => "errors.users.password.required",
+ "on" => "create"
)
),
"confirm" => array(
@@ -78,7 +79,14 @@ function passwordLength($check) {
}
public function beforeSave($options = array()) {
- $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
+ // hash password if defined... otherwise, use old password.
+ if (isset($this->data["User"]["password"]) && $this->data["User"]["password"] != null) {
+ $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
+ } else {
+ $user = $this->findById($this->data["User"]["id"]);
+ $this->data["User"]["password"] = $user["User"]["password"];
+ }
+
return true;
}
}
@@ -0,0 +1,22 @@
+<div class="row">
+ <div class="span6">
+ <?php echo $this->Form->create("Users", array("action" => "assign_roles", "class" => "form-horizontal")); ?>
+ <fieldset>
+ <legend><?php echo __("Assign Roles"); ?></legend>
+ <?php
+ echo $this->Form->hidden("User.id");
+ echo $this->TwitterBootstrap->input("User.username", array("readonly" => "readonly"));
+ echo $this->TwitterBootstrap->input("Role", array("type" => "select", "multiple" => "checkbox"));
+ ?>
+ </fieldset>
+ <div class="row form-actions">
+ <div class="span12">
+ <?php
+ echo $this->Form->button(__("Save", true), array("class" => "btn btn-primary")) . " ";
+ echo $this->Form->button(__("Reset", true), array("type" => "reset", "class" => "btn"));
+ ?>
+ </div>
+ </div>
+ <?php echo $this->Form->end(); ?>
+ </div>
+</div>
View
@@ -6,7 +6,7 @@
echo $this->Form->input('id');
echo $this->Form->input('username');
echo $this->Form->input('password', array("value" => ""));
- echo $this->Form->input('Role');
+ echo $this->Form->input('Role'); //, array("select" => "checkbox"));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit'));?>
@@ -22,4 +22,4 @@
<li><?php echo $this->Html->link(__('List Roles'), array('controller' => 'roles', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New Role'), array('controller' => 'roles', 'action' => 'add')); ?> </li>
</ul>
-</div>
+</div>
View
@@ -23,7 +23,7 @@
<td><?php echo $user['User']['modified']; ?>&nbsp;</td>
<td class="actions">
<?php echo $this->Html->link(__('View'), array('action' => 'view', $user['User']['id'])); ?>
- <?php echo $this->Html->link(__('Edit'), array('action' => 'edit', $user['User']['id'])); ?>
+ <?php echo $this->Html->link(__('Assign Roles'), array('action' => 'assign_roles', $user['User']['username'])); ?>
<?php echo $this->Html->link(__('Delete'), array('action' => 'delete', $user['User']['id']), null, sprintf(__('Are you sure you want to delete # %s?'), $user['User']['id'])); ?>
</td>
</tr>
@@ -52,4 +52,4 @@
<li><?php echo $this->Html->link(__('List Roles'), array('controller' => 'roles', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New Role'), array('controller' => 'roles', 'action' => 'add')); ?> </li>
</ul>
-</div>
+</div>

0 comments on commit 87bb079

Please sign in to comment.