summaryrefslogtreecommitdiff
path: root/manager
diff options
context:
space:
mode:
authorMichael Tänzer <neo@nhng.de>2011-06-30 21:48:35 +0200
committerMichael Tänzer <neo@nhng.de>2011-06-30 21:48:35 +0200
commit5f8e018db52a1489d67f35a4806c78b6cb131abe (patch)
tree305c05d9593444ecb6ad768815e5ee99a607a25d /manager
parent2ed178bdc3ff9e415634be8793c194d35fa66c70 (diff)
downloadcacert-mgr-5f8e018db52a1489d67f35a4806c78b6cb131abe.tar.gz
cacert-mgr-5f8e018db52a1489d67f35a4806c78b6cb131abe.tar.xz
cacert-mgr-5f8e018db52a1489d67f35a4806c78b6cb131abe.zip
Move flagsAction() functionality into the user model
Signed-off-by: Michael Tänzer <neo@nhng.de>
Diffstat (limited to 'manager')
-rw-r--r--manager/application/controllers/ManageAccountController.php36
-rw-r--r--manager/application/models/User.php66
2 files changed, 78 insertions, 24 deletions
diff --git a/manager/application/controllers/ManageAccountController.php b/manager/application/controllers/ManageAccountController.php
index f89b177..be185c4 100644
--- a/manager/application/controllers/ManageAccountController.php
+++ b/manager/application/controllers/ManageAccountController.php
@@ -136,28 +136,24 @@ class ManageAccountController extends Zend_Controller_Action
public function flagsAction()
{
- // Get user data
- $user['id'] = $this->getUserId();
+ $user = Default_Model_User::findCurrentUser();
// Validate form
- $form = $this->getFlagsForm($user['id']);
+ $form = $this->getFlagsForm($user);
$this->view->flags_form = $form;
if (!$this->getRequest()->isPost() || !$form->isValid($_POST)) {
return;
}
- $flags = array('admin', 'codesign', 'orgadmin', 'ttpadmin', 'board',
- 'locadmin', 'locked', 'assurer_blocked');
- $update = array(); // Make sure array is empty
- foreach ($flags as $flag) {
- if ($form->getElement($flag)->isChecked()) {
- $update[$flag] = 1;
- } else {
- $update[$flag] = 0;
+ $flags = $user->getFlags();
+ foreach ($flags as $flag => $value) {
+ $element = $form->getElement($flag);
+ if ($element !== null) {
+ $flags[$flag] = $element->isChecked();
}
}
- $this->db->update('users', $update, '`id` = '.$user['id']);
+ $user->setFlags($flags);
return;
}
@@ -254,22 +250,13 @@ class ManageAccountController extends Zend_Controller_Action
return $form;
}
- protected function getFlagsForm($user_id)
+ protected function getFlagsForm(Default_Model_User $user)
{
$form = new Zend_Form();
$form->setAction('/manage-account/flags')
->setMethod('post');
- // Get the current setting of the flags
- $query = 'select `admin`, `codesign`, `orgadmin`, `ttpadmin`, `board`,
- `tverify`, `locadmin`, `locked`, `assurer_blocked` from `users`
- where `id` = :user';
- $query_params['user'] = $user_id;
- $result = $this->db->query($query, $query_params);
- if ($result->rowCount() !== 1) {
- throw new Exception(__METHOD__ . ': user ID not found in the data base');
- }
- $row = $result->fetch();
+ $flags = $user->getFlags();
// Add a checkbox for each flag
$labels = array();
@@ -279,13 +266,14 @@ class ManageAccountController extends Zend_Controller_Action
$labels['ttpadmin'] = I18n::_('TTP Admin');
$labels['board'] = I18n::_('Board Member');
$labels['locadmin'] = I18n::_('Location Admin');
+ $labels['tverify'] = I18n::_('TVerify');
$labels['locked'] = I18n::_('Lock Account');
$labels['assurer_blocked'] = I18n::_('Block Assurer');
foreach ($labels as $flag => $label) {
$checkbox = new Zend_Form_Element_Checkbox($flag);
$checkbox->setLabel($label)
- ->setChecked($row[$flag] === '1');
+ ->setChecked($flags[$flag]);
$form->addElement($checkbox);
}
diff --git a/manager/application/models/User.php b/manager/application/models/User.php
index 27f8a44..b8285a4 100644
--- a/manager/application/models/User.php
+++ b/manager/application/models/User.php
@@ -406,4 +406,70 @@ class Default_Model_User {
$this->fixAssurerFlag();
}
+
+ /**
+ * Get the flags that are set
+ *
+ * @return array (string => boolean)
+ */
+ public function getFlags() {
+ $flags = $this->db->select()->from('users', self::flags())
+ ->where('`id` = ?', $this->id)->query()->fetch();
+
+ foreach ($flags as $key => $value) {
+ if ($value === '0') {
+ $flags[$key] = false;
+ } else {
+ $flags[$key] = true;
+ }
+ }
+
+ return $flags;
+ }
+
+ /**
+ * Set the flags - to know which flags exist you might want to call
+ * getFlags() first
+ *
+ * @param $flags array (string => boolean)
+ * Currently unknown flags are silently ignored
+ */
+ public function setFlags(array $flags) {
+ $newflags = array();
+
+ // filter values
+ foreach (self::flags() as $flag) {
+ if (isset($flags[$flag])) {
+ if ($flags[$flag]) {
+ $newflags[$flag] = 1;
+ } else {
+ $newflags[$flag] = 0;
+ }
+ }
+ }
+
+ $where = $this->db->quoteInto('`id` = ?', $this->id, Zend_Db::INT_TYPE);
+ $this->db->update('users', $newflags, $where);
+ }
+
+ /**
+ * The flags from the `users` table that might be set
+ */
+ private static function flags() {
+ return array(
+ 'verified',
+ 'listme',
+ 'codesign',
+ '1024bit',
+ 'admin',
+ 'orgadmin',
+ 'ttpadmin',
+ 'adadmin',
+ 'board',
+ 'tverify',
+ 'locadmin',
+ 'locked',
+ 'assurer',
+ 'assurer_blocked');
+ }
} \ No newline at end of file