From 002d34fe34e3e53980480f6f24b61e1e81652890 Mon Sep 17 00:00:00 2001 From: Peter Rotich <peter@osticket.com> Date: Thu, 10 Apr 2014 14:53:33 +0000 Subject: [PATCH] Move organization support from UserAccount to User model. Previously we required an account to associate a user to an organization. --- include/ajax.users.php | 15 +++--- include/class.organization.php | 2 +- include/class.user.php | 51 +++++++++---------- include/staff/orgs.inc.php | 2 +- include/staff/templates/tickets.tmpl.php | 2 +- include/staff/templates/users.tmpl.php | 11 ++-- include/staff/user-view.inc.php | 7 ++- include/upgrader/streams/core.sig | 2 +- .../streams/core/4323a6a8-9ef33a06.patch.sql | 2 +- .../streams/core/9ef33a06-8f99b8bf.patch.sql | 29 +++++++++++ setup/inc/streams/core/install-mysql.sql | 13 +++-- 11 files changed, 84 insertions(+), 52 deletions(-) create mode 100644 include/upgrader/streams/core/9ef33a06-8f99b8bf.patch.sql diff --git a/include/ajax.users.php b/include/ajax.users.php index 8c19ec0e7..19db8c3de 100644 --- a/include/ajax.users.php +++ b/include/ajax.users.php @@ -305,9 +305,8 @@ class UsersAjaxAPI extends AjaxController { if (!$thisstaff) Http::response(403, 'Login Required'); - elseif (!($user = User::lookup($id)) - || !($account=$user->getAccount())) - Http::response(404, 'Unknown user account'); + elseif (!($user = User::lookup($id))) + Http::response(404, 'Unknown user'); $info = array(); $info['title'] = 'Organization for '.$user->getName(); @@ -324,20 +323,20 @@ class UsersAjaxAPI extends AjaxController { $info['error'] = 'Unable to create organization - try again!'; } - if ($org && $account->setOrganization($org)) + if ($org && $user->setOrganization($org)) Http::response(201, $org->to_json()); - - $info['error'] = 'Unable to user account - try again!'; + elseif (! $info['error']) + $info['error'] = 'Unable to add organization - try again!'; } elseif ($orgId) $org = Organization::lookup($orgId); - elseif ($org = $account->getOrganization()) { + elseif ($org = $user->getOrganization()) { $info['title'] = $org->getName(); $info['action'] = $info['onselect'] = ''; $tmpl = 'org.tmpl.php'; } - if ($org && $account->getOrgId() && $org->getId() != $account->getOrgId()) + if ($org && $user->getOrgId() && $org->getId() != $user->getOrgId()) $info['warning'] = 'Are you sure you want to change user\'s organization?'; $tmpl = $tmpl ?: 'org-lookup.tmpl.php'; diff --git a/include/class.organization.php b/include/class.organization.php index c12ae9a04..a837505fa 100644 --- a/include/class.organization.php +++ b/include/class.organization.php @@ -23,7 +23,7 @@ class OrganizationModel extends VerySimpleModel { 'pk' => array('id'), 'joins' => array( 'users' => array( - 'reverse' => 'UserAccount.org', + 'reverse' => 'User.org', ), ) ); diff --git a/include/class.user.php b/include/class.user.php index 9a22b3961..9401429b1 100644 --- a/include/class.user.php +++ b/include/class.user.php @@ -67,6 +67,9 @@ class UserModel extends VerySimpleModel { 'list' => false, 'reverse' => 'UserAccount.user', ), + 'org' => array( + 'constraint' => array('org_id' => 'Organization.id') + ), 'default_email' => array( 'null' => true, 'constraint' => array('default_email_id' => 'UserEmailModel.id') @@ -85,6 +88,28 @@ class UserModel extends VerySimpleModel { function getDefaultEmail() { return $this->default_email; } + + function getAccount() { + return $this->account; + } + + function getOrgId() { + return $this->get('org_id'); + } + + function getOrganization() { + return $this->org; + } + + function setOrganization($org) { + if (!$org instanceof Organization) + return false; + + $this->set('org', $org); + $this->save(); + + return true; + } } class User extends UserModel { @@ -234,10 +259,6 @@ class User extends UserModel { return $this->_forms; } - function getAccount() { - return $this->account; - } - function getAccountStatus() { if (!($account=$this->getAccount())) @@ -534,9 +555,6 @@ class UserAccountModel extends VerySimpleModel { 'null' => false, 'constraint' => array('user_id' => 'User.id') ), - 'org' => array( - 'constraint' => array('org_id' => 'Organization.id') - ), ), ); @@ -609,25 +627,6 @@ class UserAccountModel extends VerySimpleModel { $this->user->set('account', $this); return $this->user; } - - function getOrgId() { - return $this->get('org_id'); - } - - function getOrganization() { - return $this->org; - } - - function setOrganization($org) { - if (!$org instanceof Organization) - return false; - - $this->set('org', $org); - $this->save(); - - return true; - } - } class UserAccount extends UserAccountModel { diff --git a/include/staff/orgs.inc.php b/include/staff/orgs.inc.php index ded6efeb2..4c11d1d36 100644 --- a/include/staff/orgs.inc.php +++ b/include/staff/orgs.inc.php @@ -56,7 +56,7 @@ $qstr.='&order='.($order=='DESC'?'ASC':'DESC'); $select .= ', count(DISTINCT user.id) as users '; -$from .= ' LEFT JOIN '.USER_ACCOUNT_TABLE.' user ON (user.org_id = org.id) '; +$from .= ' LEFT JOIN '.USER_TABLE.' user ON (user.org_id = org.id) '; $query="$select $from $where GROUP BY org.id ORDER BY $order_by LIMIT ".$pageNav->getStart().",".$pageNav->getLimit(); diff --git a/include/staff/templates/tickets.tmpl.php b/include/staff/templates/tickets.tmpl.php index bdd562578..abf3314ef 100644 --- a/include/staff/templates/tickets.tmpl.php +++ b/include/staff/templates/tickets.tmpl.php @@ -23,7 +23,7 @@ $from =' FROM '.TICKET_TABLE.' ticket ' if ($user) $where = 'WHERE ticket.user_id = '.db_input($user->getId()); elseif ($org) - $where = 'WHERE account.org_id = '.db_input($org->getId()); + $where = 'WHERE user.org_id = '.db_input($org->getId()); TicketForm::ensureDynamicDataView(); diff --git a/include/staff/templates/users.tmpl.php b/include/staff/templates/users.tmpl.php index 1b7ab4e1e..685b2fa85 100644 --- a/include/staff/templates/users.tmpl.php +++ b/include/staff/templates/users.tmpl.php @@ -1,14 +1,11 @@ <?php - $qstr=''; $select = 'SELECT user.*, email.address as email '; $from = 'FROM '.USER_TABLE.' user ' - . 'LEFT JOIN '.USER_ACCOUNT_TABLE.' account ON (user.id = account.user_id) ' . 'LEFT JOIN '.USER_EMAIL_TABLE.' email ON (user.id = email.user_id) '; -$where='WHERE account.org_id='.db_input($org->getId()); - +$where = ' WHERE user.org_id='.db_input($org->getId()); $sortOptions = array('name' => 'user.name', 'email' => 'email.address', @@ -53,7 +50,7 @@ $res = db_query($query); if($res && ($num=db_num_rows($res))) $showing .= $pageNav->showing(); else - $showing .= 'No users found!'; + $showing .= "This organization doesn't have any users yet"; ?> <div style="width:700px; float:left;"><b><?php echo $showing; ?></b></div> @@ -61,6 +58,8 @@ else <b><a href="#orgs/<?php echo $org->getId(); ?>/add-user" class="Icon newstaff add-user">Add New User</a></b></div> <div class="clear"></div> <br/> +<?php +if ($num) { ?> <form action="users.php" method="POST" name="staff" > <?php csrf_token(); ?> <input type="hidden" name="do" value="mass_process" > @@ -111,6 +110,8 @@ if($res && $num): //Show options.. endif; ?> </form> +<?php +} ?> <script type="text/javascript"> $(function() { diff --git a/include/staff/user-view.inc.php b/include/staff/user-view.inc.php index de27ddc2f..0e297150c 100644 --- a/include/staff/user-view.inc.php +++ b/include/staff/user-view.inc.php @@ -2,7 +2,8 @@ if(!defined('OSTSCPINC') || !$thisstaff || !is_object($user)) die('Invalid path'); $account = $user->getAccount(); -$org = $account ? $account->getOrganization() : null; +$org = $user->getOrganization(); + ?> <table width="940" cellpadding="2" cellspacing="0" border="0"> @@ -84,12 +85,10 @@ $org = $account ? $account->getOrganization() : null; echo sprintf('<a href="#users/%d/org" class="user-action">%s</a>', $user->getId(), $org->getName()); - elseif ($account) + else echo sprintf('<a href="#users/%d/org" class="user-action">Add Organization</a>', $user->getId()); - else - echo ' '; ?> </span> </td> diff --git a/include/upgrader/streams/core.sig b/include/upgrader/streams/core.sig index 267cb8e33..324095cf4 100644 --- a/include/upgrader/streams/core.sig +++ b/include/upgrader/streams/core.sig @@ -1 +1 @@ -9ef33a062ca3a20190dfad594d594a69 +8f99b8bf9bee63c8e4dc274ffbdda383 diff --git a/include/upgrader/streams/core/4323a6a8-9ef33a06.patch.sql b/include/upgrader/streams/core/4323a6a8-9ef33a06.patch.sql index 9867ef23c..d49df2d43 100644 --- a/include/upgrader/streams/core/4323a6a8-9ef33a06.patch.sql +++ b/include/upgrader/streams/core/4323a6a8-9ef33a06.patch.sql @@ -13,7 +13,7 @@ ALTER TABLE `%TABLE_PREFIX%list_items` ADD `properties` text AFTER `sort`; ALTER TABLE `%TABLE_PREFIX%organization` - ADD `status` int(11) NOT NULL DEFAULT 0 AFTER `staff_id`, + ADD `status` int(11) unsigned NOT NULL DEFAULT 0 AFTER `staff_id`, ADD `domain` varchar(128) NOT NULL DEFAULT '' AFTER `status`, ADD `extra` text AFTER `domain`; diff --git a/include/upgrader/streams/core/9ef33a06-8f99b8bf.patch.sql b/include/upgrader/streams/core/9ef33a06-8f99b8bf.patch.sql new file mode 100644 index 000000000..da9dbcd29 --- /dev/null +++ b/include/upgrader/streams/core/9ef33a06-8f99b8bf.patch.sql @@ -0,0 +1,29 @@ +/** + * @version v1.8.2 + * @signature 8f99b8bf9bee63c8e4dc274ffbdda383 + * @title Move organization support from UserAccount to User model. + * + */ + +ALTER TABLE `%TABLE_PREFIX%user` + ADD `org_id` int(11) unsigned NOT NULL AFTER `id`, + ADD `status` int(11) unsigned NOT NULL DEFAULT 0 AFTER `default_email_id`, + ADD INDEX (`org_id`); + +ALTER TABLE `%TABLE_PREFIX%user_account` + DROP `org_id`, + ADD INDEX (`user_id`); + +ALTER TABLE `%TABLE_PREFIX%ticket` + ADD INDEX (`user_id`); + +ALTER TABLE `%TABLE_PREFIX%draft` + ADD `extra` text AFTER `body`; + +ALTER TABLE `%TABLE_PREFIX%organization` + CHANGE `staff_id` `manager` varchar(16) NOT NULL DEFAULT '', + CHANGE `domain` `domain` varchar(256) NOT NULL DEFAULT ''; + +UPDATE `%TABLE_PREFIX%config` + SET `value` = '8f99b8bf9bee63c8e4dc274ffbdda383' + WHERE `key` = 'schema_signature' AND `namespace` = 'core'; diff --git a/setup/inc/streams/core/install-mysql.sql b/setup/inc/streams/core/install-mysql.sql index acd48f9bb..20c3dc346 100644 --- a/setup/inc/streams/core/install-mysql.sql +++ b/setup/inc/streams/core/install-mysql.sql @@ -203,6 +203,7 @@ CREATE TABLE `%TABLE_PREFIX%draft` ( `staff_id` int(11) unsigned NOT NULL, `namespace` varchar(32) NOT NULL DEFAULT '', `body` text NOT NULL, + `extra` text, `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `updated` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`) @@ -429,9 +430,9 @@ DROP TABLE IF EXISTS `%TABLE_PREFIX%organization`; CREATE TABLE `%TABLE_PREFIX%organization` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(128) NOT NULL DEFAULT '', - `staff_id` int(10) unsigned NOT NULL DEFAULT '0', + `manager` varchar(16) NOT NULL DEFAULT '', `status` int(11) unsigned NOT NULL DEFAULT '0', - `domain` varchar(128) NOT NULL DEFAULT '', + `domain` varchar(256) NOT NULL DEFAULT '', `extra` text, `created` timestamp NULL DEFAULT NULL, `updated` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, @@ -586,6 +587,7 @@ CREATE TABLE `%TABLE_PREFIX%ticket` ( `created` datetime NOT NULL, `updated` datetime NOT NULL, PRIMARY KEY (`ticket_id`), + KEY `user_id` (`user_id`), KEY `dept_id` (`dept_id`), KEY `staff_id` (`staff_id`), KEY `team_id` (`staff_id`), @@ -771,11 +773,14 @@ CREATE TABLE `%TABLE_PREFIX%plugin` ( DROP TABLE IF EXISTS `%TABLE_PREFIX%user`; CREATE TABLE `%TABLE_PREFIX%user` ( `id` int(10) unsigned NOT NULL auto_increment, + `org_id` int(10) unsigned NOT NULL, `default_email_id` int(10) NOT NULL, + `status` int(11) unsigned NOT NULL DEFAULT '0', `name` varchar(128) NOT NULL, `created` datetime NOT NULL, `updated` datetime NOT NULL, - PRIMARY KEY (`id`) + PRIMARY KEY (`id`), + KEY `org_id` (`org_id`) ) DEFAULT CHARSET=utf8; DROP TABLE IF EXISTS `%TABLE_PREFIX%user_email`; @@ -792,7 +797,6 @@ DROP TABLE IF EXISTS `%TABLE_PREFIX%user_account`; CREATE TABLE `%TABLE_PREFIX%user_account` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `user_id` int(10) unsigned NOT NULL, - `org_id` int(11) unsigned NOT NULL, `status` int(11) unsigned NOT NULL DEFAULT '0', `timezone_id` int(11) NOT NULL DEFAULT '0', `dst` tinyint(1) NOT NULL DEFAULT '1', @@ -802,5 +806,6 @@ CREATE TABLE `%TABLE_PREFIX%user_account` ( `backend` varchar(32) DEFAULT NULL, `registered` timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`), + KEY `user_id` (`user_id`), UNIQUE KEY `username` (`username`) ) DEFAULT CHARSET=utf8; -- GitLab