@ -19,9 +19,16 @@
package org.wso2.carbon.device.mgt.core.search.mgt.impl ;
package org.wso2.carbon.device.mgt.core.search.mgt.impl ;
import org.apache.commons.logging.Log ;
import org.apache.commons.logging.LogFactory ;
import org.wso2.carbon.context.PrivilegedCarbonContext ;
import org.wso2.carbon.device.mgt.common.Device ;
import org.wso2.carbon.device.mgt.common.Device ;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder ;
import org.wso2.carbon.device.mgt.core.permission.mgt.PermissionUtils ;
import org.wso2.carbon.device.mgt.core.search.mgt.Constants ;
import org.wso2.carbon.device.mgt.core.search.mgt.Constants ;
import org.wso2.carbon.device.mgt.core.search.mgt.ResultSetAggregator ;
import org.wso2.carbon.device.mgt.core.search.mgt.ResultSetAggregator ;
import org.wso2.carbon.user.api.UserRealm ;
import org.wso2.carbon.user.api.UserStoreException ;
import java.util.ArrayList ;
import java.util.ArrayList ;
import java.util.HashMap ;
import java.util.HashMap ;
@ -29,16 +36,19 @@ import java.util.List;
import java.util.Map ;
import java.util.Map ;
public class ResultSetAggregatorImpl implements ResultSetAggregator {
public class ResultSetAggregatorImpl implements ResultSetAggregator {
private static Log log = LogFactory . getLog ( ResultSetAggregatorImpl . class ) ;
private final static String ANY_DEVICE_PERMISSION = "/device-mgt/devices/any-device" ;
private static final String UI_EXECUTE = "ui.execute" ;
@Override
@Override
public List < Device > aggregate ( Map < String , List < Device > > devices ) {
public List < Device > aggregate ( Map < String , List < Device > > devices ) {
Map < Integer , Device > generalQueryMap = this . convertToMap ( devices . get ( Constants . GENERAL ) ) ;
Map < Integer , Device > generalQueryMap = this . convertToMap ( devices . get ( Constants . GENERAL ) ) ;
Map < Integer , Device > andMap = this . convertToMap ( devices . get ( Constants . PROP_AND ) ) ;
Map < Integer , Device > andMap = this . convertToMap ( devices . get ( Constants . PROP_AND ) ) ;
Map < Integer , Device > orMap = this . convertToMap ( devices . get ( Constants . PROP_OR ) ) ;
Map < Integer , Device > orMap = this . convertToMap ( devices . get ( Constants . PROP_OR ) ) ;
Map < Integer , Device > locationMap = this . convertToMap ( devices . get ( Constants . LOCATION ) ) ;
Map < Integer , Device > locationMap = this . convertToMap ( devices . get ( Constants . LOCATION ) ) ;
Map < Integer , Device > finalMap = new HashMap < > ( ) ;
Map < Integer , Device > finalMap = new HashMap < > ( ) ;
List < Device > finalResult = new ArrayList < > ( ) ;
List < Device > finalResult = new ArrayList < > ( ) ;
List < Device > ownDevices = new ArrayList < > ( ) ;
if ( andMap . isEmpty ( ) ) {
if ( andMap . isEmpty ( ) ) {
finalMap = generalQueryMap ;
finalMap = generalQueryMap ;
@ -70,8 +80,24 @@ public class ResultSetAggregatorImpl implements ResultSetAggregator {
}
}
}
}
String username = PrivilegedCarbonContext . getThreadLocalCarbonContext ( ) . getUsername ( ) ;
try {
if ( isPermittedToViewAnyDevice ( username ) ) {
return finalResult ;
return finalResult ;
}
}
} catch ( UserStoreException e ) {
log . error ( "Unable to check permissions of the user: " + username , e ) ;
}
for ( Device device : finalResult ) {
if ( username . equals ( device . getEnrolmentInfo ( ) . getOwner ( ) ) ) {
ownDevices . add ( device ) ;
}
}
return ownDevices ;
}
private Map < Integer , Device > convertToMap ( List < Device > devices ) {
private Map < Integer , Device > convertToMap ( List < Device > devices ) {
if ( devices = = null ) {
if ( devices = = null ) {
@ -79,7 +105,7 @@ public class ResultSetAggregatorImpl implements ResultSetAggregator {
}
}
Map < Integer , Device > deviceWrapperMap = new HashMap < > ( ) ;
Map < Integer , Device > deviceWrapperMap = new HashMap < > ( ) ;
for ( Device device : devices ) {
for ( Device device : devices ) {
deviceWrapperMap . put ( device . get Id( ) , device ) ;
deviceWrapperMap . put ( device . get EnrolmentInfo( ) . get Id( ) , device ) ;
}
}
return deviceWrapperMap ;
return deviceWrapperMap ;
}
}
@ -92,4 +118,19 @@ public class ResultSetAggregatorImpl implements ResultSetAggregator {
return list ;
return list ;
}
}
/ * *
* Checks if the user has permissions to view all devices .
*
* @param username username
* @return { @code true } if user is permitted
* @throws UserStoreException If unable to check user permissions
* /
private boolean isPermittedToViewAnyDevice ( String username ) throws UserStoreException {
int tenantId = PrivilegedCarbonContext . getThreadLocalCarbonContext ( ) . getTenantId ( true ) ;
UserRealm userRealm = DeviceManagementDataHolder . getInstance ( ) . getRealmService ( ) . getTenantUserRealm ( tenantId ) ;
return userRealm ! = null & & userRealm . getAuthorizationManager ( ) ! = null & &
userRealm . getAuthorizationManager ( ) . isUserAuthorized ( username ,
PermissionUtils . getAbsolutePermissionPath ( ANY_DEVICE_PERMISSION ) , UI_EXECUTE ) ;
}
}
}