@@ -1427,51 +1427,56 @@ public List<EventVO> searchForEvents(ListEventsCmd cmd) {
14271427 String accountName = cmd .getAccountName ();
14281428 Long domainId = cmd .getDomainId ();
14291429 Long projectId = cmd .getProjectId ();
1430-
1431- if (( caller == null ) || isAdmin (caller .getType ())) {
1430+
1431+ if (_accountMgr . isAdmin (caller .getType ())) {
14321432 isAdmin = true ;
14331433 // validate domainId before proceeding
14341434 if (domainId != null ) {
1435- if ((caller != null ) && !_domainDao .isChildDomain (caller .getDomainId (), domainId )) {
1436- throw new PermissionDeniedException ("Invalid domain id (" + domainId + ") given, unable to list events." );
1437- }
1438-
1439- if (accountName != null ) {
1440- Account userAccount = _accountDao .findAccount (accountName , domainId );
1441- if (userAccount != null ) {
1442- permittedAccounts .add (userAccount .getId ());
1443- } else {
1444- throw new InvalidParameterValueException ("Unable to find account " + accountName + " in domain " + domainId );
1445- }
1446- }
1447- } else {
1448- domainId = ((caller == null ) ? DomainVO .ROOT_DOMAIN : caller .getDomainId ());
1435+ Domain domain = _domainDao .findById (domainId );
1436+ if (domain == null ) {
1437+ throw new InvalidParameterValueException ("Unable to find domain by id " + domainId );
1438+ }
1439+ _accountMgr .checkAccess (caller , _domainDao .findById (domainId ));
1440+
14491441 if (accountName != null ) {
1450- Account userAccount = _accountDao .findAccount (accountName , domainId );
1451- if (userAccount != null ) {
1452- permittedAccounts .add (userAccount .getId ());
1453- } else {
1454- throw new InvalidParameterValueException ("DomainId is not specified. Unable to find account " + accountName + " in default root domain " + domainId );
1442+ Account userAccount = _accountDao .findNonProjectAccountIncludingRemoved (accountName , domainId );
1443+ if (userAccount == null ) {
1444+ throw new InvalidParameterValueException ("Unable to find account " + accountName + " in domain " + domainId );
14551445 }
1446+
1447+ permittedAccounts .add (userAccount .getId ());
14561448 }
1449+ } else {
1450+ domainId = caller .getDomainId ();
1451+ if (accountName != null ) {
1452+ Account userAccount = _accountDao .findNonProjectAccountIncludingRemoved (accountName , domainId );
1453+ if (userAccount == null ) {
1454+ throw new InvalidParameterValueException ("Can't find account " + accountName + " in domain id=" + domainId );
1455+ }
1456+ permittedAccounts .add (userAccount .getId ());
1457+ }
14571458 }
14581459 } else {
14591460 permittedAccounts .add (caller .getId ());
14601461 }
14611462
14621463 //set project information
1464+ boolean skipProjectEvents = true ;
14631465 if (projectId != null ) {
1464- permittedAccounts .clear ();
1465- Project project = _projectMgr .getProject (projectId );
1466- if (project == null ) {
1467- throw new InvalidParameterValueException ("Unable to find project by id " + projectId );
1468- }
1469- if (!_projectMgr .canAccessProjectAccount (caller , project .getProjectAccountId ())) {
1470- throw new InvalidParameterValueException ("Account " + caller + " can't access project id=" + projectId );
1471- }
1472- permittedAccounts .add (project .getProjectAccountId ());
1473- } else if (caller .getType () == Account .ACCOUNT_TYPE_NORMAL ){
1474- permittedAccounts .addAll (_projectMgr .listPermittedProjectAccounts (caller .getId ()));
1466+ if (projectId == -1 ) {
1467+ permittedAccounts .addAll (_projectMgr .listPermittedProjectAccounts (caller .getId ()));
1468+ } else {
1469+ permittedAccounts .clear ();
1470+ Project project = _projectMgr .getProject (projectId );
1471+ if (project == null ) {
1472+ throw new InvalidParameterValueException ("Unable to find project by id " + projectId );
1473+ }
1474+ if (!_projectMgr .canAccessProjectAccount (caller , project .getProjectAccountId ())) {
1475+ throw new InvalidParameterValueException ("Account " + caller + " can't access project id=" + projectId );
1476+ }
1477+ permittedAccounts .add (project .getProjectAccountId ());
1478+ }
1479+ skipProjectEvents = false ;
14751480 }
14761481
14771482 Filter searchFilter = new Filter (EventVO .class , "createDate" , false , cmd .getStartIndex (), cmd .getPageSizeVal ());
@@ -1503,43 +1508,42 @@ public List<EventVO> searchForEvents(ListEventsCmd cmd) {
15031508 sb .and ("createDateB" , sb .entity ().getCreateDate (), SearchCriteria .Op .BETWEEN );
15041509 sb .and ("createDateG" , sb .entity ().getCreateDate (), SearchCriteria .Op .GTEQ );
15051510 sb .and ("createDateL" , sb .entity ().getCreateDate (), SearchCriteria .Op .LTEQ );
1511+ sb .and ("accountType" , sb .entity ().getAccountType (), SearchCriteria .Op .NEQ );
15061512
1507- if (( permittedAccounts . isEmpty ()) && ( accountName == null ) && ( domainId != null ) && isAdmin ) {
1508- // if accountId isn't specified, we can do a domain match for the admin case
1509- SearchBuilder <DomainVO > domainSearch = _domainDao .createSearchBuilder ();
1510- domainSearch .and ("path" , domainSearch .entity ().getPath (), SearchCriteria .Op .LIKE );
1511- sb .join ("domainSearch" , domainSearch , sb .entity ().getDomainId (), domainSearch .entity ().getId (), JoinBuilder .JoinType .INNER );
1513+ if (isAdmin && permittedAccounts . isEmpty ( ) && domainId != null ) {
1514+ // if accountId isn't specified, we can do a domain match for the admin case
1515+ SearchBuilder <DomainVO > domainSearch = _domainDao .createSearchBuilder ();
1516+ domainSearch .and ("path" , domainSearch .entity ().getPath (), SearchCriteria .Op .LIKE );
1517+ sb .join ("domainSearch" , domainSearch , sb .entity ().getDomainId (), domainSearch .entity ().getId (), JoinBuilder .JoinType .INNER );
15121518 }
1513-
1519+
15141520 SearchCriteria <EventVO > sc = sb .create ();
1521+ if (!permittedAccounts .isEmpty ()) {
1522+ sc .setParameters ("accountId" , permittedAccounts .toArray ());
1523+ } else if (domainId != null ) {
1524+ sc .setJoinParameters ("domainSearch" , "path" , _domainDao .findById (domainId ).getPath () + "%" );
1525+ }
1526+
1527+ if (skipProjectEvents ) {
1528+ sc .setParameters ("accountType" , Account .ACCOUNT_TYPE_PROJECT );
1529+ }
1530+
15151531 if (id != null ) {
15161532 sc .setParameters ("id" , id );
15171533 }
1534+
15181535 if (keyword != null ) {
15191536 SearchCriteria <EventVO > ssc = _eventDao .createSearchCriteria ();
15201537 ssc .addOr ("type" , SearchCriteria .Op .LIKE , "%" + keyword + "%" );
15211538 ssc .addOr ("description" , SearchCriteria .Op .LIKE , "%" + keyword + "%" );
15221539 ssc .addOr ("level" , SearchCriteria .Op .LIKE , "%" + keyword + "%" );
1523-
15241540 sc .addAnd ("level" , SearchCriteria .Op .SC , ssc );
15251541 }
15261542
15271543 if (level != null ) {
15281544 sc .setParameters ("levelEQ" , level );
15291545 }
15301546
1531- if (!permittedAccounts .isEmpty ()) {
1532- sc .setParameters ("accountId" , permittedAccounts .toArray ());
1533- } else if (domainId != null ) {
1534- if (accountName != null ) {
1535- sc .setParameters ("domainIdEQ" , domainId );
1536- sc .setParameters ("accountName" , "%" + accountName + "%" );
1537- } else if (isAdmin ) {
1538- DomainVO domain = _domainDao .findById (domainId );
1539- sc .setJoinParameters ("domainSearch" , "path" , domain .getPath () + "%" );
1540- }
1541- }
1542-
15431547 if (type != null ) {
15441548 sc .setParameters ("type" , type );
15451549 }
0 commit comments