@ -25,9 +25,10 @@ import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.device.mgt.common.Device ;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier ;
import org.wso2.carbon.device.mgt.common.DeviceManagementException ;
import org.wso2.carbon.device.mgt.common.PaginationRe ques t;
import org.wso2.carbon.device.mgt.common.PaginationRe sul t;
import org.wso2.carbon.device.mgt.common.TransactionManagementException ;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup ;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyEixistException ;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException ;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupUser ;
import org.wso2.carbon.device.mgt.core.group.mgt.DeviceGroupBuilder ;
@ -64,8 +65,8 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* { @inheritDoc }
* /
@Override
public int createGroup ( DeviceGroup deviceGroup , String defaultRole , String [ ] defaultPermissions )
throws GroupManagementException {
public void createGroup ( DeviceGroup deviceGroup , String defaultRole , String [ ] defaultPermissions )
throws GroupManagementException , GroupAlreadyEixistException {
if ( deviceGroup = = null ) {
throw new GroupManagementException ( "DeviceGroup cannot be null." , new NullPointerException ( ) ) ;
}
@ -74,16 +75,12 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
int groupId = - 1 ;
try {
GroupManagementDAOFactory . beginTransaction ( ) ;
boolean nameIsExists = this . groupDAO . isGroupExist ( deviceGroup . getName ( ) , tenantId) ;
boolean nameIsExists = this . groupDAO . isGroupExist ( deviceGroup . getName ( ) , deviceGroup. getOwner ( ) , tenantId) ;
if ( ! nameIsExists ) {
groupId = this . groupDAO . addGroup ( groupBroker , tenantId ) ;
GroupManagementDAOFactory . commitTransaction ( ) ;
if ( groupId < 0 ) {
return - 1 ;
}
groupBroker . setId ( groupId ) ;
} else {
return - 2 ;
throw new GroupAlreadyEixistException ( "Group exist with name " + deviceGroup . getName ( ) ) ;
}
} catch ( GroupManagementDAOException e ) {
GroupManagementDAOFactory . rollbackTransaction ( ) ;
@ -99,20 +96,20 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
if ( log . isDebugEnabled ( ) ) {
log . debug ( "DeviceGroup added: " + groupBroker . getName ( ) ) ;
}
return groupId ;
}
/ * *
* { @inheritDoc }
* /
@Override
public void updateGroup ( DeviceGroup deviceGroup ) throws GroupManagementException {
public void updateGroup ( DeviceGroup deviceGroup , String oldGroupName ) throws GroupManagementException {
if ( deviceGroup = = null ) {
throw new GroupManagementException ( "DeviceGroup cannot be null." , new NullPointerException ( ) ) ;
}
try {
int tenantId = CarbonContext . getThreadLocalCarbonContext ( ) . getTenantId ( ) ;
GroupManagementDAOFactory . beginTransaction ( ) ;
this . groupDAO . updateGroup ( deviceGroup , CarbonContext. getThreadLocalCarbonContext ( ) . getTenantId ( ) ) ;
this . groupDAO . updateGroup ( deviceGroup , oldGroupName, tenantId ) ;
GroupManagementDAOFactory . commitTransaction ( ) ;
} catch ( GroupManagementDAOException e ) {
GroupManagementDAOFactory . rollbackTransaction ( ) ;
@ -129,22 +126,22 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* { @inheritDoc }
* /
@Override
public boolean deleteGroup ( int groupId ) throws GroupManagementException {
public boolean deleteGroup ( String groupName , String owner ) throws GroupManagementException {
String roleName ;
DeviceGroup deviceGroup = getGroup ( groupId ) ;
DeviceGroup Builder deviceGroup = getGroupBuilder ( groupName , owner ) ;
if ( deviceGroup = = null ) {
return false ;
}
List < String > groupRoles = getRoles ( group Id ) ;
List < String > groupRoles = getRoles ( group Name, owner ) ;
for ( String role : groupRoles ) {
if ( role ! = null ) {
roleName = role . replace ( "Internal/group-" + groupId + "-" , "" ) ;
removeGroupSharingRole ( groupId, roleName ) ;
roleName = role . replace ( "Internal/group-" + deviceGroup. getG roupId( ) + "-" , "" ) ;
removeGroupSharingRole ( deviceGroup. getG roupId( ) , roleName ) ;
}
}
try {
GroupManagementDAOFactory . beginTransaction ( ) ;
this . groupDAO . deleteGroup ( group Id , CarbonContext . getThreadLocalCarbonContext ( ) . getTenantId ( ) ) ;
this . groupDAO . deleteGroup ( group Name, owner , CarbonContext . getThreadLocalCarbonContext ( ) . getTenantId ( ) ) ;
GroupManagementDAOFactory . commitTransaction ( ) ;
if ( log . isDebugEnabled ( ) ) {
log . debug ( "DeviceGroup " + deviceGroup . getName ( ) + " removed." ) ;
@ -153,7 +150,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
} catch ( GroupManagementDAOException e ) {
GroupManagementDAOFactory . rollbackTransaction ( ) ;
throw new GroupManagementException ( "Error occurred while removing group " +
"'" + group Id + "' data." , e ) ;
"'" + group Name + "' data." , e ) ;
} catch ( TransactionManagementException e ) {
throw new GroupManagementException ( "Error occurred while initiating transaction." , e ) ;
} finally {
@ -165,25 +162,49 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* { @inheritDoc }
* /
@Override
public DeviceGroup getGroup ( int groupId ) throws GroupManagementException {
public DeviceGroup getGroup ( String groupName , String owner ) throws GroupManagementException {
return getGroupBuilder ( groupName , owner ) . getGroup ( ) ;
}
@SuppressWarnings ( "Duplicates" )
private DeviceGroupBuilder getGroupBuilder ( String groupName , String owner ) throws GroupManagementException {
DeviceGroupBuilder deviceGroupBuilder ;
try {
GroupManagementDAOFactory . openConnection ( ) ;
int tenantId = CarbonContext . getThreadLocalCarbonContext ( ) . getTenantId ( ) ;
deviceGroupBuilder = this . groupDAO . getGroup ( groupName , owner , tenantId ) ;
} catch ( GroupManagementDAOException e ) {
throw new GroupManagementException ( "Error occurred while obtaining group '" + groupName + "'" , e ) ;
} catch ( SQLException e ) {
throw new GroupManagementException ( "Error occurred while opening a connection to the data source." , e ) ;
} finally {
GroupManagementDAOFactory . closeConnection ( ) ;
}
if ( deviceGroupBuilder ! = null ) {
deviceGroupBuilder . setUsers ( this . getUsers ( deviceGroupBuilder . getGroupId ( ) ) ) ;
deviceGroupBuilder . setRoles ( this . getRoles ( deviceGroupBuilder . getGroupId ( ) ) ) ;
}
return deviceGroupBuilder ;
}
@SuppressWarnings ( "Duplicates" )
private DeviceGroupBuilder getGroupBuilder ( int groupId ) throws GroupManagementException {
DeviceGroupBuilder groupBroker ;
try {
GroupManagementDAOFactory . openConnection ( ) ;
groupBroker = this . groupDAO . getGroup ( groupId , CarbonContext . getThreadLocalCarbonContext ( ) . getTenantId ( ) ) ;
} catch ( GroupManagementDAOException e ) {
throw new GroupManagementException ( "Error occurred while obtaining group " + groupId , e ) ;
throw new GroupManagementException ( "Error occurred while obtaining group ' " + groupId + "'" , e ) ;
} catch ( SQLException e ) {
throw new GroupManagementException ( "Error occurred while opening a connection to the data source." , e ) ;
} finally {
GroupManagementDAOFactory . closeConnection ( ) ;
}
if ( groupBroker ! = null ) {
groupBroker . setUsers ( this . getUsers ( groupId ) ) ;
groupBroker . setRoles ( this . getRoles ( groupId ) ) ;
return groupBroker . getGroup ( ) ;
} else {
return null ;
groupBroker . setUsers ( this . getUsers ( groupBroker . getGroupId ( ) ) ) ;
groupBroker . setRoles ( this . getRoles ( groupBroker . getGroupId ( ) ) ) ;
}
return groupBroker ;
}
/ * *
@ -205,20 +226,20 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
}
List < DeviceGroup > groupsWithData = new ArrayList < > ( ) ;
for ( DeviceGroupBuilder groupBroker : deviceGroups ) {
groupBroker . setUsers ( this . getUsers ( groupBroker . get Id( ) ) ) ;
groupBroker . setRoles ( this . getRoles ( groupBroker . get Id( ) ) ) ;
groupBroker . setUsers ( this . getUsers ( groupBroker . get Group Id( ) ) ) ;
groupBroker . setRoles ( this . getRoles ( groupBroker . get Group Id( ) ) ) ;
groupsWithData . add ( groupBroker . getGroup ( ) ) ;
}
return groupsWithData ;
}
@Override
public List< DeviceGroup > getGroups ( PaginationRequest reques t) throws GroupManagementException {
public PaginationResult getGroups ( int startIndex , int rowCoun t) throws GroupManagementException {
List < DeviceGroupBuilder > deviceGroups = new ArrayList < > ( ) ;
try {
int tenantId = CarbonContext . getThreadLocalCarbonContext ( ) . getTenantId ( ) ;
GroupManagementDAOFactory . openConnection ( ) ;
deviceGroups = this . groupDAO . getGroups ( reques t, tenantId ) ;
deviceGroups = this . groupDAO . getGroups ( startIndex, rowCoun t, tenantId ) ;
} catch ( GroupManagementDAOException e ) {
throw new GroupManagementException ( "Error occurred while retrieving all groups in tenant" , e ) ;
} catch ( SQLException e ) {
@ -228,21 +249,23 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
}
List < DeviceGroup > groupsWithData = new ArrayList < > ( ) ;
for ( DeviceGroupBuilder groupBroker : deviceGroups ) {
groupBroker . setUsers ( this . getUsers ( groupBroker . get Id( ) ) ) ;
groupBroker . setRoles ( this . getRoles ( groupBroker . get Id( ) ) ) ;
groupBroker . setUsers ( this . getUsers ( groupBroker . get Group Id( ) ) ) ;
groupBroker . setRoles ( this . getRoles ( groupBroker . get Group Id( ) ) ) ;
groupsWithData . add ( groupBroker . getGroup ( ) ) ;
}
return groupsWithData ;
PaginationResult paginationResult = new PaginationResult ( ) ;
paginationResult . setRecordsTotal ( getGroupCount ( ) ) ;
paginationResult . setData ( groupsWithData ) ;
paginationResult . setRecordsFiltered ( groupsWithData . size ( ) ) ;
return paginationResult ;
}
@Override
public int getGroupCount ( ) throws GroupManagementException {
try {
int count ;
int tenantId = CarbonContext . getThreadLocalCarbonContext ( ) . getTenantId ( ) ;
GroupManagementDAOFactory . openConnection ( ) ;
count = groupDAO . getGroupCount ( tenantId ) ;
return count ;
return groupDAO . getGroupCount ( tenantId ) ;
} catch ( GroupManagementDAOException e ) {
throw new GroupManagementException ( "Error occurred while retrieving all groups in tenant" , e ) ;
} catch ( SQLException e ) {
@ -266,9 +289,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
Map < Integer , DeviceGroup > groups = new HashMap < > ( ) ;
for ( String role : roleList ) {
if ( role ! = null & & role . contains ( "Internal/group-" ) ) {
DeviceGroup deviceGroup = extractNewGroupFromRole ( groups , role ) ;
if ( deviceGroup ! = null ) {
groups . put ( deviceGroup . get Id( ) , deviceGroup ) ;
DeviceGroup Builder deviceGroup Builder = extractNewGroupFromRole ( groups , role ) ;
if ( deviceGroup Builder ! = null ) {
groups . put ( deviceGroup Builder . get Group Id( ) , deviceGroup Builder. getGroup ( ) ) ;
}
}
}
@ -283,15 +306,36 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* /
@Override
public int getGroupCount ( String username ) throws GroupManagementException {
return this . getGroups ( username ) . size ( ) ;
UserStoreManager userStoreManager ;
int count = 0 ;
try {
int tenantId = CarbonContext . getThreadLocalCarbonContext ( ) . getTenantId ( ) ;
userStoreManager = DeviceManagementDataHolder . getInstance ( ) . getRealmService ( ) . getTenantUserRealm ( tenantId )
. getUserStoreManager ( ) ;
String [ ] roleList = userStoreManager . getRoleListOfUser ( username ) ;
List < Integer > groupIds = new ArrayList < > ( ) ;
for ( String role : roleList ) {
if ( role ! = null & & role . contains ( "Internal/group-" ) ) {
int groupId = Integer . parseInt ( role . split ( "-" ) [ 1 ] ) ;
if ( ! groupIds . contains ( groupId ) ) {
groupIds . add ( groupId ) ;
count + + ;
}
}
}
return count ;
} catch ( UserStoreException e ) {
throw new GroupManagementException ( "Error occurred while getting user store manager." , e ) ;
}
}
/ * *
* { @inheritDoc }
* /
@Override
public boolean shareGroup ( String username , int groupId , String sharingRole )
public boolean shareGroup ( String username , String groupName , String owner , String sharingRole )
throws GroupManagementException {
int groupId = getGroupId ( groupName , owner ) ;
return modifyGroupShare ( username , groupId , sharingRole , true ) ;
}
@ -299,26 +343,66 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* { @inheritDoc }
* /
@Override
public boolean unshareGroup ( String username , int groupId , String sharingRole )
public boolean unshareGroup ( String username , String groupName , String owner , String sharingRole )
throws GroupManagementException {
int groupId = getGroupId ( groupName , owner ) ;
return modifyGroupShare ( username , groupId , sharingRole , false ) ;
}
private boolean modifyGroupShare ( String username , int groupId , String sharingRole ,
boolean isAddNew )
throws GroupManagementException {
if ( groupId = = - 1 ) {
return false ;
}
UserStoreManager userStoreManager ;
String [ ] roles = new String [ 1 ] ;
try {
int tenantId = CarbonContext . getThreadLocalCarbonContext ( ) . getTenantId ( ) ;
userStoreManager =
DeviceManagementDataHolder . getInstance ( ) . getRealmService ( ) . getTenantUserRealm (
tenantId ) . getUserStoreManager ( ) ;
roles [ 0 ] = "Internal/group-" + groupId + "-" + sharingRole ;
if ( isAddNew ) {
userStoreManager . updateRoleListOfUser ( username , null , roles ) ;
} else {
userStoreManager . updateRoleListOfUser ( username , roles , null ) ;
}
return true ;
} catch ( UserStoreException e ) {
throw new GroupManagementException ( "User store error in adding user " + username + " to group name:" +
groupId , e ) ;
}
}
private int getGroupId ( String groupName , String owner ) throws GroupManagementException {
DeviceGroupBuilder deviceGroupBuilder = getGroupBuilder ( groupName , owner ) ;
if ( deviceGroupBuilder = = null ) {
return - 1 ;
}
return deviceGroupBuilder . getGroupId ( ) ;
}
/ * *
* { @inheritDoc }
* /
@Override
public boolean addGroupSharingRole ( String username , int groupId , String roleName ,
public boolean addGroupSharingRole ( String userName , String groupName , String owner , String roleName ,
String [ ] permissions ) throws GroupManagementException {
int groupId = getGroupId ( groupName , owner ) ;
return addGroupSharingRole ( userName , groupId , roleName , permissions ) ;
}
private boolean addGroupSharingRole ( String username , int groupId , String roleName ,
String [ ] permissions )
throws GroupManagementException {
if ( groupId = = - 1 ) {
return false ;
}
UserStoreManager userStoreManager ;
String role ;
String [ ] userNames = new String [ 1 ] ;
try {
DeviceGroup deviceGroup = getGroup ( groupId ) ;
if ( deviceGroup = = null ) {
return false ;
}
int tenantId = CarbonContext . getThreadLocalCarbonContext ( ) . getTenantId ( ) ;
userStoreManager = DeviceManagementDataHolder . getInstance ( ) . getRealmService ( ) . getTenantUserRealm ( tenantId )
. getUserStoreManager ( ) ;
@ -340,15 +424,20 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* { @inheritDoc }
* /
@Override
public boolean removeGroupSharingRole ( int groupId , String roleName )
public boolean removeGroupSharingRole ( String groupName , String owner , String roleName )
throws GroupManagementException {
int groupId = getGroupId ( groupName , owner ) ;
return removeGroupSharingRole ( groupId , roleName ) ;
}
private boolean removeGroupSharingRole ( int groupId , String roleName )
throws GroupManagementException {
if ( groupId = = - 1 ) {
return false ;
}
UserStoreManager userStoreManager ;
String role ;
try {
DeviceGroup deviceGroup = getGroup ( groupId ) ;
if ( deviceGroup = = null ) {
return false ;
}
int tenantId = CarbonContext . getThreadLocalCarbonContext ( ) . getTenantId ( ) ;
userStoreManager = DeviceManagementDataHolder . getInstance ( ) . getRealmService ( ) . getTenantUserRealm ( tenantId )
. getUserStoreManager ( ) ;
@ -366,7 +455,12 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* { @inheritDoc }
* /
@Override
public List < String > getRoles ( int groupId ) throws GroupManagementException {
public List < String > getRoles ( String groupName , String owner ) throws GroupManagementException {
int groupId = getGroupId ( groupName , owner ) ;
return getRoles ( groupId ) ;
}
private List < String > getRoles ( int groupId ) throws GroupManagementException {
UserStoreManager userStoreManager ;
String [ ] roles ;
List < String > groupRoles ;
@ -393,7 +487,12 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* { @inheritDoc }
* /
@Override
public List < String > getRoles ( String username , int groupId ) throws GroupManagementException {
public List < String > getRoles ( String username , String groupName , String owner ) throws GroupManagementException {
int groupId = getGroupId ( groupName , owner ) ;
return getRoles ( username , groupId ) ;
}
private List < String > getRoles ( String username , int groupId ) throws GroupManagementException {
UserStoreManager userStoreManager ;
List < String > groupRoleList = new ArrayList < > ( ) ;
try {
@ -417,6 +516,11 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* { @inheritDoc }
* /
@Override
public List < GroupUser > getUsers ( String groupName , String owner ) throws GroupManagementException {
int groupId = getGroupId ( groupName , owner ) ;
return getUsers ( groupId ) ;
}
public List < GroupUser > getUsers ( int groupId ) throws GroupManagementException {
UserStoreManager userStoreManager ;
Map < String , GroupUser > groupUserHashMap = new HashMap < > ( ) ;
@ -453,11 +557,11 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* { @inheritDoc }
* /
@Override
public List < Device > getDevices ( int groupId ) throws GroupManagementException {
public List < Device > getDevices ( String groupName , String owner ) throws GroupManagementException {
try {
int tenantId = CarbonContext . getThreadLocalCarbonContext ( ) . getTenantId ( ) ;
GroupManagementDAOFactory . getConnection ( ) ;
return this . groupDAO . getDevices ( group Id , tenantId ) ;
return this . groupDAO . getDevices ( group Name, owner , tenantId ) ;
} catch ( GroupManagementDAOException e ) {
throw new GroupManagementException ( "Error occurred while getting devices in group." , e ) ;
} catch ( SQLException e ) {
@ -471,12 +575,13 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* { @inheritDoc }
* /
@Override
public List< Device > getDevices ( int groupId , PaginationRequest reques t)
public PaginationResult getDevices ( String groupName , String owner , int startIndex , int rowCoun t)
throws GroupManagementException {
int tenantId = CarbonContext . getThreadLocalCarbonContext ( ) . getTenantId ( ) ;
List < Device > devices ;
try {
GroupManagementDAOFactory . getConnection ( ) ;
return this . groupDAO . getDevices ( groupId , reques t, tenantId ) ;
devices = this . groupDAO . getDevices ( groupName , owner , startIndex , rowCoun t, tenantId ) ;
} catch ( GroupManagementDAOException e ) {
throw new GroupManagementException ( "Error occurred while getting devices in group." , e ) ;
} catch ( SQLException e ) {
@ -484,17 +589,22 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
} finally {
GroupManagementDAOFactory . closeConnection ( ) ;
}
PaginationResult paginationResult = new PaginationResult ( ) ;
paginationResult . setRecordsTotal ( getDeviceCount ( groupName , owner ) ) ;
paginationResult . setData ( devices ) ;
paginationResult . setRecordsFiltered ( devices . size ( ) ) ;
return paginationResult ;
}
/ * *
* { @inheritDoc }
* /
@Override
public int getDeviceCount ( int groupId ) throws GroupManagementException {
public int getDeviceCount ( String groupName , String owner ) throws GroupManagementException {
try {
int count ;
GroupManagementDAOFactory . getConnection ( ) ;
count = groupDAO . getDeviceCount ( group Id ,
count = groupDAO . getDeviceCount ( group Name, owner ,
CarbonContext . getThreadLocalCarbonContext ( ) . getTenantId ( ) ) ;
return count ;
} catch ( GroupManagementDAOException e ) {
@ -510,25 +620,23 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* { @inheritDoc }
* /
@Override
public boolean addDevice ( DeviceIdentifier deviceIdentifier , int groupId )
public boolean addDevice ( DeviceIdentifier deviceIdentifier , String groupName , String owner )
throws GroupManagementException {
Device device ;
DeviceGroup deviceGroup ;
try {
device = DeviceManagementDataHolder . getInstance ( ) . getDeviceManagementProvider ( ) . getDevice ( deviceIdentifier ) ;
deviceGroup = this . getGroup ( groupId ) ;
if ( device = = null | | deviceGroup = = null ) {
if ( device = = null ) {
return false ;
}
int tenantId = CarbonContext . getThreadLocalCarbonContext ( ) . getTenantId ( ) ;
GroupManagementDAOFactory . beginTransaction ( ) ;
this . groupDAO . addDevice ( group Id , device . getId ( ) , tenantId ) ;
this . groupDAO . addDevice ( group Name, owner , device . getId ( ) , tenantId ) ;
GroupManagementDAOFactory . commitTransaction ( ) ;
} catch ( DeviceManagementException e ) {
throw new GroupManagementException ( "Error occurred while retrieving device." , e ) ;
} catch ( GroupManagementDAOException e ) {
GroupManagementDAOFactory . rollbackTransaction ( ) ;
throw new GroupManagementException ( "Error occurred while adding device to group '" + group Id + "'." , e ) ;
throw new GroupManagementException ( "Error occurred while adding device to group '" + group Name + "'." , e ) ;
} catch ( TransactionManagementException e ) {
throw new GroupManagementException ( "Error occurred while initiating transaction." , e ) ;
} finally {
@ -541,19 +649,17 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* { @inheritDoc }
* /
@Override
public boolean removeDevice ( DeviceIdentifier deviceIdentifier , int groupId )
public boolean removeDevice ( DeviceIdentifier deviceIdentifier , String groupName , String owner )
throws GroupManagementException {
Device device ;
DeviceGroup deviceGroup ;
try {
device = DeviceManagementDataHolder . getInstance ( ) . getDeviceManagementProvider ( ) . getDevice ( deviceIdentifier ) ;
deviceGroup = this . getGroup ( groupId ) ;
if ( device = = null | | deviceGroup = = null ) {
if ( device = = null ) {
return false ;
}
int tenantId = CarbonContext . getThreadLocalCarbonContext ( ) . getTenantId ( ) ;
GroupManagementDAOFactory . beginTransaction ( ) ;
this . groupDAO . removeDevice ( group Id , device . getId ( ) , tenantId ) ;
this . groupDAO . removeDevice ( group Name, owner , device . getId ( ) , tenantId ) ;
GroupManagementDAOFactory . commitTransaction ( ) ;
} catch ( DeviceManagementException e ) {
throw new GroupManagementException ( "Error occurred while retrieving device." , e ) ;
@ -561,7 +667,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
throw new GroupManagementException ( "Error occurred while initiating transaction." , e ) ;
} catch ( GroupManagementDAOException e ) {
GroupManagementDAOFactory . rollbackTransaction ( ) ;
throw new GroupManagementException ( "Error occurred while adding device to group '" + group Id + "'." , e ) ;
throw new GroupManagementException ( "Error occurred while adding device to group '" + group Name + "'." , e ) ;
} finally {
GroupManagementDAOFactory . closeConnection ( ) ;
}
@ -572,8 +678,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* { @inheritDoc }
* /
@Override
public String [ ] getPermissions ( String username , int groupId ) throws GroupManagementException {
public String [ ] getPermissions ( String username , String groupName , String owner ) throws GroupManagementException {
UserRealm userRealm ;
int groupId = getGroupId ( groupName , owner ) ;
List < String > roles = getRoles ( username , groupId ) ;
int tenantId = CarbonContext . getThreadLocalCarbonContext ( ) . getTenantId ( ) ;
try {
@ -613,9 +720,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
for ( String role : roles ) {
if ( role ! = null & & role . contains ( "Internal/group-" ) & & userRealm . getAuthorizationManager ( )
. isRoleAuthorized ( role , permission , CarbonConstants . UI_PERMISSION_ACTION ) ) {
DeviceGroup deviceGroup = extractNewGroupFromRole ( groups , role ) ;
if ( deviceGroup ! = null ) {
groups . put ( deviceGroup . get Id( ) , deviceGroup ) ;
DeviceGroup Builder deviceGroup Builder = extractNewGroupFromRole ( groups , role ) ;
if ( deviceGroup Builder ! = null ) {
groups . put ( deviceGroup Builder . get Group Id( ) , deviceGroup Builder. getGroup ( ) ) ;
}
}
}
@ -625,63 +732,12 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
}
}
/ * *
* { @inheritDoc }
* /
@Override
public boolean isAuthorized ( String username , int groupId , String permission )
throws GroupManagementException {
UserRealm userRealm ;
int tenantId = CarbonContext . getThreadLocalCarbonContext ( ) . getTenantId ( ) ;
try {
userRealm = DeviceManagementDataHolder . getInstance ( ) . getRealmService ( ) . getTenantUserRealm ( tenantId ) ;
List < String > roles = this . getRoles ( username , groupId ) ;
for ( String role : roles ) {
if ( userRealm . getAuthorizationManager ( )
. isRoleAuthorized ( "Internal/group-" + groupId + "-" + role , permission ,
CarbonConstants . UI_PERMISSION_ACTION ) ) {
return true ;
}
}
return false ;
} catch ( UserStoreException e ) {
throw new GroupManagementException ( "Error occurred while getting user realm." , e ) ;
}
}
private boolean modifyGroupShare ( String username , int groupId , String sharingRole ,
boolean isAddNew )
throws GroupManagementException {
UserStoreManager userStoreManager ;
String [ ] roles = new String [ 1 ] ;
try {
DeviceGroup deviceGroup = getGroup ( groupId ) ;
if ( deviceGroup = = null ) {
return false ;
}
int tenantId = CarbonContext . getThreadLocalCarbonContext ( ) . getTenantId ( ) ;
userStoreManager =
DeviceManagementDataHolder . getInstance ( ) . getRealmService ( ) . getTenantUserRealm (
tenantId ) . getUserStoreManager ( ) ;
roles [ 0 ] = "Internal/group-" + groupId + "-" + sharingRole ;
if ( isAddNew ) {
userStoreManager . updateRoleListOfUser ( username , null , roles ) ;
} else {
userStoreManager . updateRoleListOfUser ( username , roles , null ) ;
}
return true ;
} catch ( UserStoreException e ) {
throw new GroupManagementException ( "User store error in adding user " + username + " to group name:" +
groupId , e ) ;
}
}
private DeviceGroup extractNewGroupFromRole ( Map < Integer , DeviceGroup > groups , String role )
private DeviceGroupBuilder extractNewGroupFromRole ( Map < Integer , DeviceGroup > groups , String role )
throws GroupManagementException {
try {
int groupId = Integer . parseInt ( role . split ( "-" ) [ 1 ] ) ;
if ( ! groups . containsKey ( groupId ) ) {
return getGroup ( groupId ) ;
return getGroupBuilder ( groupId ) ;
}
} catch ( NumberFormatException e ) {
log . error ( "Unable to extract groupId from role " + role , e ) ;