2012-03-01 17:24:29 +01:00
|
|
|
/**
|
|
|
|
* EGroupware clientside API object
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Andreas Stöckel (as AT stylite.de)
|
|
|
|
* @author Ralf Becker <RalfBecker@outdoor-training.de>
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*egw:uses
|
|
|
|
egw_core;
|
|
|
|
*/
|
|
|
|
|
2014-03-28 09:06:30 +01:00
|
|
|
egw.extend('user', egw.MODULE_GLOBAL, function()
|
|
|
|
{
|
2012-03-01 17:24:29 +01:00
|
|
|
/**
|
|
|
|
* Data about current user
|
2014-03-28 09:06:30 +01:00
|
|
|
*
|
2012-03-01 17:24:29 +01:00
|
|
|
* @access: private, use egw.user(_field) or egw.app(_app)
|
|
|
|
*/
|
2014-03-28 14:15:11 +01:00
|
|
|
var userData = {apps: {}};
|
2012-03-01 17:24:29 +01:00
|
|
|
|
2014-04-23 21:00:59 +02:00
|
|
|
/**
|
|
|
|
* Client side cache of accounts user has access to
|
|
|
|
* Used by account select widgets
|
|
|
|
*/
|
|
|
|
var accountStore = {
|
|
|
|
// Filled by AJAX when needed
|
|
|
|
//accounts: {},
|
|
|
|
//groups: {},
|
|
|
|
//owngroups: {}
|
|
|
|
};
|
|
|
|
|
2012-03-01 17:24:29 +01:00
|
|
|
return {
|
|
|
|
/**
|
|
|
|
* Set data of current user
|
2014-03-28 09:06:30 +01:00
|
|
|
*
|
|
|
|
* @param {object} _data
|
2015-02-02 20:49:18 +01:00
|
|
|
* @param {boolean} _need_clone _data need to be cloned, as it is from different window context
|
|
|
|
* and therefore will be inaccessible in IE, after that window is closed
|
2012-03-01 17:24:29 +01:00
|
|
|
*/
|
2015-02-02 20:49:18 +01:00
|
|
|
set_user: function(_data, _need_clone)
|
2012-03-01 17:24:29 +01:00
|
|
|
{
|
2015-02-02 20:49:18 +01:00
|
|
|
userData = _need_clone ? jQuery.extend(true, {}, _data) : _data;
|
2012-03-01 17:24:29 +01:00
|
|
|
},
|
2014-03-28 09:06:30 +01:00
|
|
|
|
2012-03-01 17:24:29 +01:00
|
|
|
/**
|
|
|
|
* Get data about current user
|
|
|
|
*
|
2014-03-28 09:06:30 +01:00
|
|
|
* @param {string} _field
|
2012-03-01 17:24:29 +01:00
|
|
|
* - 'account_id','account_lid','person_id','account_status',
|
|
|
|
* - 'account_firstname','account_lastname','account_email','account_fullname','account_phone'
|
|
|
|
* - 'apps': object with app => data pairs the user has run-rights for
|
2014-03-28 09:06:30 +01:00
|
|
|
* @return {string|array|null}
|
2012-03-01 17:24:29 +01:00
|
|
|
*/
|
|
|
|
user: function (_field)
|
|
|
|
{
|
2014-03-28 09:06:30 +01:00
|
|
|
return userData[_field];
|
2012-03-01 17:24:29 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return data of apps the user has rights to run
|
2014-03-28 09:06:30 +01:00
|
|
|
*
|
2012-03-01 17:24:29 +01:00
|
|
|
* Can be used the check of run rights like: if (egw.app('addressbook')) { do something if user has addressbook rights }
|
2014-03-28 09:06:30 +01:00
|
|
|
*
|
|
|
|
* @param {string} _app
|
|
|
|
* @param {string} _name attribute to return, default return whole app-data-object
|
2012-03-01 17:24:29 +01:00
|
|
|
* @return object|string|null null if not found
|
|
|
|
*/
|
|
|
|
app: function(_app, _name)
|
|
|
|
{
|
2014-03-28 09:06:30 +01:00
|
|
|
return typeof _name == 'undefined' || typeof userData.apps[_app] == 'undefined' ?
|
|
|
|
userData.apps[_app] : userData.apps[_app][_name];
|
2014-04-23 21:00:59 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of accounts the user has access to
|
|
|
|
* The list is filtered by type, one of 'accounts','groups','both', 'owngroups'
|
|
|
|
*
|
|
|
|
* @param {string} type
|
2014-05-07 17:51:45 +02:00
|
|
|
* @returns {array}
|
2014-04-23 21:00:59 +02:00
|
|
|
*/
|
|
|
|
accounts: function(type)
|
|
|
|
{
|
|
|
|
if(typeof type == 'undefined') type = 'accounts';
|
|
|
|
|
2014-05-07 17:51:45 +02:00
|
|
|
var list = [];
|
2014-04-23 21:00:59 +02:00
|
|
|
if(jQuery.isEmptyObject(accountStore))
|
|
|
|
{
|
|
|
|
// Synchronous
|
|
|
|
egw.json('home.egw_framework.ajax_user_list.template',[],
|
2015-02-02 20:49:18 +01:00
|
|
|
function(data) {
|
|
|
|
accountStore = jQuery.extend(true, {}, data||{});
|
|
|
|
}
|
2014-04-23 21:00:59 +02:00
|
|
|
).sendRequest();
|
|
|
|
}
|
|
|
|
if(type == 'both')
|
|
|
|
{
|
2014-05-07 17:51:45 +02:00
|
|
|
list = list.concat(accountStore['accounts'], accountStore['groups']);
|
2014-04-23 21:00:59 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-07 17:51:45 +02:00
|
|
|
list = list.concat(accountStore[type]);
|
2014-04-23 21:00:59 +02:00
|
|
|
}
|
|
|
|
return list;
|
2014-06-26 10:23:02 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invalidate client-side account cache
|
|
|
|
*
|
|
|
|
* For _type == "add" we invalidate the whole cache currently.
|
|
|
|
*
|
|
|
|
* @param {number} _id nummeric account_id, !_id will invalidate whole cache
|
|
|
|
* @param {string} _type "add", "delete", "update" or "edit"
|
|
|
|
*/
|
|
|
|
invalidate_account: function(_id, _type)
|
|
|
|
{
|
|
|
|
if (jQuery.isEmptyObject(accountStore)) return;
|
|
|
|
|
|
|
|
switch(_type)
|
|
|
|
{
|
|
|
|
case 'delete':
|
|
|
|
case 'edit':
|
|
|
|
case 'update':
|
|
|
|
if (_id)
|
|
|
|
{
|
|
|
|
var store = _id < 0 ? accountStore.groups : accountStore.accounts;
|
|
|
|
for(var i=0; i < store.length; ++i)
|
|
|
|
{
|
2014-10-01 17:53:31 +02:00
|
|
|
if (store && typeof store[i] != 'undefined' && _id == store[i].value)
|
2014-06-26 10:23:02 +02:00
|
|
|
{
|
|
|
|
if (_type == 'delete')
|
|
|
|
{
|
|
|
|
delete(store[i]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.link_title('home-accounts', _id, function(_label)
|
|
|
|
{
|
|
|
|
store[i].label = _label;
|
|
|
|
if (_id < 0)
|
|
|
|
{
|
|
|
|
for(var j=0; j < accountStore.owngroups.length; ++j)
|
|
|
|
{
|
|
|
|
if (_id == accountStore.owngroups[j].value)
|
|
|
|
{
|
|
|
|
accountStore.owngroups[j].label = _label;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, this, true); // true = force reload
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// fall through
|
|
|
|
default:
|
|
|
|
accountStore = {};
|
|
|
|
break;
|
|
|
|
}
|
2012-03-01 17:24:29 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|