diff --git a/include/ajax.orgs.php b/include/ajax.orgs.php index def54cf3c549d98188a801a78199e52fd6b9e1ab..2e4b5009af19505a8a76a270ad35761ff74e74e2 100644 --- a/include/ajax.orgs.php +++ b/include/ajax.orgs.php @@ -101,6 +101,57 @@ class OrgsAjaxAPI extends AjaxController { include(STAFFINC_DIR . 'templates/org-delete.tmpl.php'); } + function addUser($id, $userId=0) { + global $thisstaff; + + if (!$thisstaff) + Http::response(403, 'Login Required'); + elseif (!($org = Organization::lookup($id))) + Http::response(404, 'Unknown organization'); + + $info = array(); + $info['title'] = 'Add User'; + $info['action'] = '#orgs/'.$org->getId().'/add-user'; + $info['onselect'] = 'ajax.php/orgs/'.$org->getId().'/add-user/'; + + $info['lookup'] = false; + if (AuthenticationBackend::getSearchDirectories()) + $info['lookup'] = 'remote'; + + if ($_POST) { + if ($_POST['id']) { //Existing useer + if (!($user = User::lookup($_POST['id']))) + $info['error'] = 'Unknown user selected'; + elseif ($user->getOrgId() == $org->getId()) + $info['error'] = sprintf('%s already belongs to the organization', + Format::htmlchars($user->getName())); + } else { //Creating new user + $form = UserForm::getUserForm()->getForm($_POST); + if (!($user = User::fromForm($form))) + $info['error'] = 'Error adding user - try again!'; + } + + if (!$info['error'] && $user && $user->setOrganization($org)) + Http::response(201, $user->to_json()); + elseif (!$info['error']) + $info['error'] = 'Unable to add user to the organization - try again'; + + } elseif ($userId) //Selected local user + $user = User::lookup($userId); + + if ($user && $user->getOrgId()) { + if ($user->getOrgId() == $org->getId()) + $info['warn'] = 'User already belongs to this organization!'; + else + $info['warn'] = "Are you sure you want to change the user's organization?"; + } + + ob_start(); + include(STAFFINC_DIR . 'templates/user-lookup.tmpl.php'); + $resp = ob_get_contents(); + ob_end_clean(); + return $resp; + } function addOrg() { diff --git a/include/staff/templates/user-lookup.tmpl.php b/include/staff/templates/user-lookup.tmpl.php index 5978242d2effdbce631f8330c97c396f47b7b800..d21d457d2d1c6fe1bb7d6236bfdad276998111a8 100644 --- a/include/staff/templates/user-lookup.tmpl.php +++ b/include/staff/templates/user-lookup.tmpl.php @@ -15,6 +15,8 @@ if (!isset($info['lookup']) || $info['lookup'] !== false) { ?> if ($info['error']) { echo sprintf('<p id="msg_error">%s</p>', $info['error']); +} elseif ($info['warn']) { + echo sprintf('<p id="msg_warning">%s</p>', $info['warn']); } elseif ($info['msg']) { echo sprintf('<p id="msg_notice">%s</p>', $info['msg']); } ?> @@ -24,9 +26,14 @@ if ($info['error']) { <i class="icon-user icon-4x pull-left icon-border"></i> <a class="action-button pull-right" style="overflow:inherit" id="unselect-user" href="#"><i class="icon-remove"></i> Add New User</a> - <div><strong id="user-name"><?php echo $user ? Format::htmlchars($user->getName()->getOriginal()) : ''; ?></strong></div> - <div><<span id="user-email"><?php echo $user ? $user->getEmail() : ''; ?></span>></div> <?php if ($user) { ?> + <div><strong id="user-name"><?php echo Format::htmlchars($user->getName()->getOriginal()); ?></strong></div> + <div><<span id="user-email"><?php echo $user->getEmail(); ?></span>></div> + <?php + if ($org=$user->getOrganization()) { ?> + <div><span id="user-org"><?php echo $org->getName(); ?></span></div> + <?php + } ?> <table style="margin-top: 1em;"> <?php foreach ($user->getDynamicData() as $entry) { ?> <tr><td colspan="2" style="border-bottom: 1px dotted black"><strong><?php @@ -99,6 +106,7 @@ $(function() { $('a#unselect-user').click( function(e) { e.preventDefault(); + $("#msg_error, #msg_notice, #msg_warning").fadeOut(); $('div#selected-user-info').hide(); $('div#new-user-form').fadeIn({start: function(){ $('#user-search').focus(); }}); return false; diff --git a/include/staff/templates/user.tmpl.php b/include/staff/templates/user.tmpl.php index 6c91f3b26416d0b0684a2fc9b1d7efa0ca4cc001..cacd574b395c60c4e196fcd8c474fb88a98b302f 100644 --- a/include/staff/templates/user.tmpl.php +++ b/include/staff/templates/user.tmpl.php @@ -22,6 +22,11 @@ if ($info['error']) { <div><b><a href="#" id="edituser"><i class="icon-edit"></i> <?php echo Format::htmlchars($user->getName()->getOriginal()); ?></a></b></div> <div><<?php echo $user->getEmail(); ?>></div> + <?php + if (($org=$user->getOrganization())) { ?> + <div><?php echo $org->getName(); ?></div> + <?php + } ?> <table style="margin-top: 1em;"> <?php foreach ($user->getDynamicData() as $entry) { ?> diff --git a/include/staff/templates/users.tmpl.php b/include/staff/templates/users.tmpl.php index 685b2fa8535557953fa54425e807a04db6952fc2..f0be96907b6a7d52333c989a274ed86073cf573e 100644 --- a/include/staff/templates/users.tmpl.php +++ b/include/staff/templates/users.tmpl.php @@ -117,8 +117,8 @@ endif; $(function() { $(document).on('click', 'a.add-user', function(e) { e.preventDefault(); - $.userLookup('ajax.php/users/add', function (user) { - window.location.href = 'users.php?id='+user.id; + $.userLookup('ajax.php/orgs/<?php echo $org->getId(); ?>/add-user', function (user) { + window.location.href = 'orgs.php?id=<?php echo $org->getId(); ?>' }); return false; diff --git a/scp/ajax.php b/scp/ajax.php index 5a340bbce92122a5eb79af30a2269e43ac857989..8385b50c718d5e246f054df1cf6119edaa4c507b 100644 --- a/scp/ajax.php +++ b/scp/ajax.php @@ -103,6 +103,8 @@ $dispatcher = patterns('', url_post('^/add$', 'addOrg'), url_get('^/select$', 'selectOrg'), url_get('^/select/(?P<id>\d+)$', 'selectOrg'), + url_get('^/(?P<id>\d+)/add-user(?:/(?P<userid>\d+))?$', 'addUser'), + url_post('^/(?P<id>\d+)/add-user$', 'addUser'), url_get('^/(?P<id>\d+)/delete$', 'delete'), url_delete('^/(?P<id>\d+)/delete$', 'delete') )),