use of com.zimbra.cs.session.AdminSession in project zm-mailbox by Zimbra.
the class SearchAccounts method handle.
@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
ZimbraSoapContext zsc = getZimbraSoapContext(context);
Provisioning prov = Provisioning.getInstance();
String query = request.getAttribute(AdminConstants.E_QUERY);
int limit = (int) request.getAttributeLong(AdminConstants.A_LIMIT, Integer.MAX_VALUE);
if (limit == 0)
limit = Integer.MAX_VALUE;
int offset = (int) request.getAttributeLong(AdminConstants.A_OFFSET, 0);
String domain = request.getAttribute(AdminConstants.A_DOMAIN, null);
boolean applyCos = request.getAttributeBool(AdminConstants.A_APPLY_COS, true);
String attrsStr = request.getAttribute(AdminConstants.A_ATTRS, null);
String sortBy = request.getAttribute(AdminConstants.A_SORT_BY, null);
String types = request.getAttribute(AdminConstants.A_TYPES, "accounts");
boolean sortAscending = request.getAttributeBool(AdminConstants.A_SORT_ASCENDING, true);
String[] attrs = attrsStr == null ? null : attrsStr.split(",");
// Note: isDomainAdminOnly *always* returns false for pure ACL based AccessManager
if (isDomainAdminOnly(zsc)) {
if (domain == null) {
domain = getAuthTokenAccountDomain(zsc).getName();
} else {
checkDomainRight(zsc, domain, AdminRight.PR_ALWAYS_ALLOW);
}
}
Domain d = null;
if (domain != null) {
d = prov.get(Key.DomainBy.name, domain);
if (d == null)
throw AccountServiceException.NO_SUCH_DOMAIN(domain);
}
AdminAccessControl aac = AdminAccessControl.getAdminAccessControl(zsc);
AdminAccessControl.SearchDirectoryRightChecker rightChecker = new AdminAccessControl.SearchDirectoryRightChecker(aac, prov, null);
SearchDirectoryOptions searchOpts = new SearchDirectoryOptions(d, attrs);
searchOpts.setTypes(types);
searchOpts.setSortOpt(sortAscending ? SortOpt.SORT_ASCENDING : SortOpt.SORT_DESCENDING);
searchOpts.setSortAttr(sortBy);
searchOpts.setFilterString(FilterId.ADMIN_SEARCH, query);
List<NamedEntry> accounts;
int limitMax = offset + limit;
AdminSession session = (AdminSession) getSession(zsc, Session.Type.ADMIN);
if (session != null) {
accounts = session.searchDirectory(searchOpts, offset, rightChecker);
} else {
accounts = prov.searchDirectory(searchOpts);
accounts = rightChecker.getAllowed(accounts, limitMax);
}
Element response = zsc.createElement(AdminConstants.SEARCH_ACCOUNTS_RESPONSE);
int numAccounts;
for (numAccounts = offset; numAccounts < limitMax && numAccounts < accounts.size(); numAccounts++) {
NamedEntry entry = accounts.get(numAccounts);
SearchDirectory.encodeEntry(prov, response, entry, applyCos, null, aac);
}
response.addAttribute(AdminConstants.A_MORE, numAccounts < accounts.size());
response.addAttribute(AdminConstants.A_SEARCH_TOTAL, accounts.size());
return response;
}
use of com.zimbra.cs.session.AdminSession in project zm-mailbox by Zimbra.
the class SearchDirectory method handle.
@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
ZimbraSoapContext zsc = getZimbraSoapContext(context);
Provisioning prov = Provisioning.getInstance();
String query = request.getAttribute(AdminConstants.E_QUERY, null);
int maxResults = (int) request.getAttributeLong(AdminConstants.A_MAX_RESULTS, MAX_SEARCH_RESULTS);
int limit = (int) request.getAttributeLong(AdminConstants.A_LIMIT, Integer.MAX_VALUE);
if (limit == 0) {
limit = Integer.MAX_VALUE;
}
int offset = (int) request.getAttributeLong(AdminConstants.A_OFFSET, 0);
String domain = request.getAttribute(AdminConstants.A_DOMAIN, null);
boolean applyCos = request.getAttributeBool(AdminConstants.A_APPLY_COS, true);
boolean applyConfig = request.getAttributeBool(AdminConstants.A_APPLY_CONFIG, true);
String origAttrsStr = request.getAttribute(AdminConstants.A_ATTRS, null);
String sortBy = request.getAttribute(AdminConstants.A_SORT_BY, null);
String types = request.getAttribute(AdminConstants.A_TYPES, "accounts");
boolean sortAscending = request.getAttributeBool(AdminConstants.A_SORT_ASCENDING, true);
boolean isCountOnly = request.getAttributeBool(AdminConstants.A_COUNT_ONLY, false);
Set<SearchDirectoryOptions.ObjectType> objTypes = SearchDirectoryOptions.ObjectType.fromCSVString(types);
// cannot specify a domain with the "coses" flag
if (objTypes.contains(SearchDirectoryOptions.ObjectType.coses) && (domain != null)) {
throw ServiceException.INVALID_REQUEST("cannot specify domain with coses flag", null);
}
// add zimbraMailTransport if account is requested
// it is needed for figuring out if the account is an "external"(not yet migrated) account.
String attrsStr = origAttrsStr;
if (objTypes.contains(SearchDirectoryOptions.ObjectType.accounts) && attrsStr != null && !attrsStr.contains(Provisioning.A_zimbraMailTransport)) {
attrsStr = attrsStr + "," + Provisioning.A_zimbraMailTransport;
}
if (((objTypes.contains(SearchDirectoryOptions.ObjectType.distributionlists)) || (objTypes.contains(SearchDirectoryOptions.ObjectType.dynamicgroups))) && (attrsStr != null) && !attrsStr.contains(Provisioning.A_memberURL)) {
attrsStr = attrsStr + "," + Provisioning.A_memberURL;
}
String[] attrs = attrsStr == null ? null : attrsStr.split(",");
Set<String> reqAttrs = attrs == null ? null : new HashSet(Arrays.asList(attrs));
Element response = zsc.createElement(AdminConstants.SEARCH_DIRECTORY_RESPONSE);
// Note: isDomainAdminOnly *always* returns false for pure ACL based AccessManager
if (isDomainAdminOnly(zsc)) {
if (objTypes.contains(SearchDirectoryOptions.ObjectType.domains)) {
if (query != null && query.length() > 0) {
throw ServiceException.PERM_DENIED("cannot search for domains");
} else {
domain = getAuthTokenAccountDomain(zsc).getName();
Domain d = null;
if (domain != null) {
d = prov.get(Key.DomainBy.name, domain);
if (d == null)
throw AccountServiceException.NO_SUCH_DOMAIN(domain);
}
GetDomain.encodeDomain(response, d, applyConfig, reqAttrs, null);
response.addAttribute(AdminConstants.A_MORE, false);
response.addAttribute(AdminConstants.A_SEARCH_TOTAL, 1);
return response;
}
}
if (objTypes.contains(SearchDirectoryOptions.ObjectType.coses))
throw ServiceException.PERM_DENIED("cannot search for coses");
if (domain == null) {
domain = getAuthTokenAccountDomain(zsc).getName();
} else {
checkDomainRight(zsc, domain, AdminRight.PR_ALWAYS_ALLOW);
}
}
Domain d = null;
if (domain != null) {
d = prov.get(Key.DomainBy.name, domain);
if (d == null)
throw AccountServiceException.NO_SUCH_DOMAIN(domain);
}
AdminAccessControl aac = AdminAccessControl.getAdminAccessControl(zsc);
AdminAccessControl.SearchDirectoryRightChecker rightChecker = new AdminAccessControl.SearchDirectoryRightChecker(aac, prov, reqAttrs);
List<NamedEntry> accounts;
AdminSession session = (AdminSession) getSession(zsc, Session.Type.ADMIN);
SearchDirectoryOptions options = new SearchDirectoryOptions();
options.setDomain(d);
options.setTypes(types);
options.setMaxResults(maxResults);
options.setFilterString(FilterId.ADMIN_SEARCH, query);
options.setReturnAttrs(attrs);
options.setSortOpt(sortAscending ? SortOpt.SORT_ASCENDING : SortOpt.SORT_DESCENDING);
options.setSortAttr(sortBy);
// query must be already RFC 2254 escaped
options.setConvertIDNToAscii(true);
// bug 36017.
// defaults, if requested, are set when accounts are paged back to the SOAP client.
// objects returned from searchDirectory are not cached anywhere.
options.setMakeObjectOpt(MakeObjectOpt.NO_DEFAULTS);
int limitMax = offset + limit;
if (session != null) {
accounts = session.searchDirectory(options, offset, rightChecker);
} else {
accounts = prov.searchDirectory(options);
accounts = rightChecker.getAllowed(accounts, limitMax);
}
if (isCountOnly) {
response.addAttribute(AdminConstants.A_NUM, accounts.size());
} else {
// use originally requested attrs for encoding
String[] origAttrs = origAttrsStr == null ? null : origAttrsStr.split(",");
Set<String> origReqAttrs = origAttrs == null ? null : Sets.newHashSet(Arrays.asList(origAttrs));
long start = System.currentTimeMillis();
int numEntries;
for (numEntries = offset; numEntries < limitMax && numEntries < accounts.size(); numEntries++) {
NamedEntry entry = accounts.get(numEntries);
boolean applyDefault = true;
if (entry instanceof Account) {
applyDefault = applyCos;
setAccountDefaults((Account) entry);
} else if (entry instanceof Domain) {
applyDefault = applyConfig;
}
encodeEntry(prov, response, entry, applyDefault, origReqAttrs, aac);
}
if (ZimbraLog.search.isTraceEnabled()) {
ZimbraLog.search.trace("SearchDirectory - encoding entries %s i=%s", ZimbraLog.elapsedTime(start, System.currentTimeMillis()), numEntries);
}
response.addAttribute(AdminConstants.A_MORE, numEntries < accounts.size());
response.addAttribute(AdminConstants.A_SEARCH_TOTAL, accounts.size());
}
return response;
}
use of com.zimbra.cs.session.AdminSession in project zm-mailbox by Zimbra.
the class GetAllMailboxes method handle.
@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
ZimbraSoapContext zsc = getZimbraSoapContext(context);
// allow only system admin for now
checkRight(zsc, context, null, AdminRight.PR_SYSTEM_ADMIN_ONLY);
int limit = (int) request.getAttributeLong(AdminConstants.A_LIMIT, Integer.MAX_VALUE);
if (limit == 0)
limit = Integer.MAX_VALUE;
int offset = (int) request.getAttributeLong(AdminConstants.A_OFFSET, 0);
/* sorting not supported for now
String sortBy = request.getAttribute(AdminConstants.A_SORT_BY, SORT_TOTAL_USED);
final boolean sortAscending = request.getAttributeBool(AdminConstants.A_SORT_ASCENDING, false);
if (!sortBy.equals(SORT_TOTAL_USED))
throw ServiceException.INVALID_REQUEST("sortBy must be " + SORT_TOTAL_USED, null);
*/
MailboxesParams params = new MailboxesParams();
List<Mailbox.MailboxData> mailboxes;
AdminSession session = (AdminSession) getSession(zsc, Session.Type.ADMIN);
if (session != null) {
MailboxesParams cachedParams = (MailboxesParams) session.getData(GET_ALL_MAILBOXES_CACHE_KEY);
if (cachedParams == null || !cachedParams.equals(params)) {
mailboxes = params.doSearch();
session.setData(GET_ALL_MAILBOXES_CACHE_KEY, params);
} else {
mailboxes = cachedParams.doSearch();
}
} else {
mailboxes = params.doSearch();
}
Element response = zsc.createElement(AdminConstants.GET_ALL_MAILBOXES_RESPONSE);
int i, limitMax = offset + limit;
for (i = offset; i < limitMax && i < mailboxes.size(); i++) {
Mailbox.MailboxData mailbox = mailboxes.get(i);
Element mbx = response.addElement(AdminConstants.E_MAILBOX);
mbx.addAttribute(AdminConstants.A_MT_ID, mailbox.id);
mbx.addAttribute(AdminConstants.A_MT_GROUPID, mailbox.schemaGroupId);
mbx.addAttribute(AdminConstants.A_MT_ACCOUNTID, mailbox.accountId);
mbx.addAttribute(AdminConstants.A_MT_INDEXVOLUMEID, mailbox.indexVolumeId);
mbx.addAttribute(AdminConstants.A_MT_ITEMIDCHECKPOINT, mailbox.lastItemId);
mbx.addAttribute(AdminConstants.A_MT_CONTACTCOUNT, mailbox.contacts);
mbx.addAttribute(AdminConstants.A_MT_SIZECHECKPOINT, mailbox.size);
mbx.addAttribute(AdminConstants.A_MT_CHANGECHECKPOINT, mailbox.lastChangeId);
mbx.addAttribute(AdminConstants.A_MT_TRACKINGSYNC, mailbox.trackSync);
mbx.addAttribute(AdminConstants.A_MT_TRACKINGIMAP, mailbox.trackImap);
if (mailbox.lastBackupDate > 0)
mbx.addAttribute(AdminConstants.A_MT_LASTBACKUPAT, mailbox.lastBackupDate);
// mbx.addAttribute(AdminConstants.A_MT_COMMENT, mailbox.comment);
mbx.addAttribute(AdminConstants.A_MT_LASTSOAPACCESS, mailbox.lastWriteDate);
mbx.addAttribute(AdminConstants.A_MT_NEWNESSAGES, mailbox.recentMessages);
}
response.addAttribute(AdminConstants.A_MORE, i < mailboxes.size());
response.addAttribute(AdminConstants.A_SEARCH_TOTAL, mailboxes.size());
return response;
}
use of com.zimbra.cs.session.AdminSession in project zm-mailbox by Zimbra.
the class GetSessions method handle.
@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
ZimbraSoapContext zsc = getZimbraSoapContext(context);
Server localServer = Provisioning.getInstance().getLocalServer();
checkRight(zsc, context, localServer, Admin.R_getSessions);
Element response = zsc.createElement(AdminConstants.GET_SESSIONS_RESPONSE);
String typeStr = request.getAttribute(AdminConstants.A_TYPE);
Type type;
try {
type = Type.valueOf(typeStr.toUpperCase());
} catch (IllegalArgumentException e) {
throw ServiceException.INVALID_REQUEST("invalid session type: " + typeStr, e);
}
SortBy sortBy = SortBy.fromString(request.getAttribute(AdminConstants.A_SORT_BY, SortBy.nameAsc.name()));
long offset = request.getAttributeLong(AdminConstants.A_OFFSET, 0);
long limit = request.getAttributeLong(AdminConstants.A_LIMIT, Integer.MAX_VALUE);
boolean refresh = request.getAttributeBool(AdminConstants.A_REFRESH, false);
AdminSession adminSession = (AdminSession) getSession(zsc, Session.Type.ADMIN);
CachedResult result = getResult(adminSession, type, refresh, sortBy);
List<SessionInfo> sessions = result.sessions;
int i, limitMax = (int) (offset + limit);
for (i = (int) offset; i < limitMax && i < sessions.size(); i++) {
sessions.get(i).encodeSession(response);
}
response.addAttribute(AdminConstants.A_MORE, i < sessions.size());
response.addAttribute(AdminConstants.A_TOTAL, sessions.size());
return response;
}
use of com.zimbra.cs.session.AdminSession in project zm-mailbox by Zimbra.
the class GetSessions method getResult.
public CachedResult getResult(AdminSession adminSession, Type type, boolean refresh, final SortBy sortBy) {
List<Session> sessions = SessionCache.getActiveSessions(type);
CachedResult result = (adminSession == null || refresh) ? null : (CachedResult) adminSession.getData(SESSION_KEY);
if (result != null && result.type == type && result.sortBy == sortBy)
return result;
Provisioning prov = Provisioning.getInstance();
result = new CachedResult();
result.type = type;
result.sortBy = sortBy;
result.sessions = new ArrayList<SessionInfo>(sessions.size());
for (Session s : sessions) {
result.sessions.add(new SessionInfo(s, getName(prov, s.getAuthenticatedAccountId())));
}
// SORT
Comparator<SessionInfo> comparator = new Comparator<SessionInfo>() {
public int compare(SessionInfo a, SessionInfo b) {
long diff;
switch(sortBy) {
case nameAsc:
return a.getAccountName().compareToIgnoreCase(b.getAccountName());
case nameDesc:
return -a.getAccountName().compareToIgnoreCase(b.getAccountName());
case accessedAsc:
diff = a.getAccessed() - b.getAccessed();
return diff == 0 ? 0 : diff > 0 ? 1 : -1;
case accessedDesc:
diff = a.getAccessed() - b.getAccessed();
return diff == 0 ? 0 : diff > 0 ? -1 : 1;
case createdAsc:
diff = a.getAccessed() - b.getAccessed();
return diff == 0 ? 0 : diff > 0 ? 1 : -1;
case createdDesc:
diff = a.getAccessed() - b.getAccessed();
return diff == 0 ? 0 : diff > 0 ? -1 : 1;
default:
return 0;
}
}
};
Collections.sort(result.sessions, comparator);
if (adminSession != null)
adminSession.setData(SESSION_KEY, result);
return result;
}
Aggregations