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