|
|
@ -28,6 +28,7 @@ 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.DeviceNotFoundException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceNotFoundException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.GroupPaginationRequest;
|
|
|
|
import org.wso2.carbon.device.mgt.common.GroupPaginationRequest;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.PaginationResult;
|
|
|
|
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.GroupAlreadyExistException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException;
|
|
|
@ -139,7 +140,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public boolean deleteGroup(int groupId) throws GroupManagementException {
|
|
|
|
public boolean deleteGroup(int groupId) throws GroupManagementException {
|
|
|
|
String roleName;
|
|
|
|
String roleName;
|
|
|
|
DeviceGroup deviceGroup = buildDeviceGroup(groupId);
|
|
|
|
DeviceGroup deviceGroup = getGroup(groupId);
|
|
|
|
if (deviceGroup == null) {
|
|
|
|
if (deviceGroup == null) {
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -168,7 +169,11 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private DeviceGroup buildDeviceGroup(int groupId) throws GroupManagementException {
|
|
|
|
/**
|
|
|
|
|
|
|
|
* {@inheritDoc}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public DeviceGroup getGroup(int groupId) throws GroupManagementException {
|
|
|
|
DeviceGroup deviceGroup;
|
|
|
|
DeviceGroup deviceGroup;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
GroupManagementDAOFactory.openConnection();
|
|
|
|
GroupManagementDAOFactory.openConnection();
|
|
|
@ -187,19 +192,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
|
|
|
|
return deviceGroup;
|
|
|
|
return deviceGroup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* {@inheritDoc}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public DeviceGroup getGroup(int groupId) throws GroupManagementException {
|
|
|
|
|
|
|
|
DeviceGroup deviceGroup = this.buildDeviceGroup(groupId);
|
|
|
|
|
|
|
|
if (deviceGroup != null) {
|
|
|
|
|
|
|
|
deviceGroup.setUsers(this.getUsers(groupId));
|
|
|
|
|
|
|
|
deviceGroup.setRoles(this.getRoles(groupId));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return deviceGroup;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public List<DeviceGroup> getGroups() throws GroupManagementException {
|
|
|
|
public List<DeviceGroup> getGroups() throws GroupManagementException {
|
|
|
|
List<DeviceGroup> deviceGroups = new ArrayList<>();
|
|
|
|
List<DeviceGroup> deviceGroups = new ArrayList<>();
|
|
|
@ -222,9 +214,21 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public List<DeviceGroup> getGroups(GroupPaginationRequest request) throws GroupManagementException {
|
|
|
|
public PaginationResult getGroups(GroupPaginationRequest request) throws GroupManagementException {
|
|
|
|
List<DeviceGroup> deviceGroups = new ArrayList<>();
|
|
|
|
|
|
|
|
request = DeviceManagerUtil.validateGroupListPageSize(request);
|
|
|
|
request = DeviceManagerUtil.validateGroupListPageSize(request);
|
|
|
|
|
|
|
|
List<DeviceGroup> deviceGroups = getPlainDeviceGroups(request);
|
|
|
|
|
|
|
|
for (DeviceGroup group : deviceGroups) {
|
|
|
|
|
|
|
|
group.setUsers(this.getUsers(group.getGroupId()));
|
|
|
|
|
|
|
|
group.setRoles(this.getRoles(group.getGroupId()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PaginationResult groupResult = new PaginationResult();
|
|
|
|
|
|
|
|
groupResult.setData(deviceGroups);
|
|
|
|
|
|
|
|
groupResult.setRecordsTotal(getGroupCount(request));
|
|
|
|
|
|
|
|
return groupResult;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private List<DeviceGroup> getPlainDeviceGroups(GroupPaginationRequest request) throws GroupManagementException {
|
|
|
|
|
|
|
|
List<DeviceGroup> deviceGroups = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
|
|
|
|
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
|
|
|
|
GroupManagementDAOFactory.openConnection();
|
|
|
|
GroupManagementDAOFactory.openConnection();
|
|
|
@ -236,10 +240,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
GroupManagementDAOFactory.closeConnection();
|
|
|
|
GroupManagementDAOFactory.closeConnection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (DeviceGroup group : deviceGroups) {
|
|
|
|
|
|
|
|
group.setUsers(this.getUsers(group.getGroupId()));
|
|
|
|
|
|
|
|
group.setRoles(this.getRoles(group.getGroupId()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return deviceGroups;
|
|
|
|
return deviceGroups;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -254,7 +254,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
|
|
|
|
String[] roleList = userStoreManager.getRoleListOfUser(username);
|
|
|
|
String[] roleList = userStoreManager.getRoleListOfUser(username);
|
|
|
|
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 deviceGroup = checkAndExtractNonExistingGroup(groups, role);
|
|
|
|
if (deviceGroup != null) {
|
|
|
|
if (deviceGroup != null) {
|
|
|
|
groups.put(deviceGroup.getGroupId(), deviceGroup);
|
|
|
|
groups.put(deviceGroup.getGroupId(), deviceGroup);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -266,30 +266,46 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
|
|
|
|
return new ArrayList<>(groups.values());
|
|
|
|
return new ArrayList<>(groups.values());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<DeviceGroup> getGroups(String currentUser, GroupPaginationRequest request) throws GroupManagementException {
|
|
|
|
public PaginationResult getGroups(String currentUser, GroupPaginationRequest request) throws GroupManagementException {
|
|
|
|
request = DeviceManagerUtil.validateGroupListPageSize(request);
|
|
|
|
request = DeviceManagerUtil.validateGroupListPageSize(request);
|
|
|
|
Map<Integer, DeviceGroup> groups = new HashMap<>();
|
|
|
|
int startIndex = request.getStartIndex();
|
|
|
|
UserStoreManager userStoreManager;
|
|
|
|
int count = request.getRowCount();
|
|
|
|
|
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
request.setRowCount(0);
|
|
|
|
|
|
|
|
List<DeviceGroup> allMatchingGroups = this.getPlainDeviceGroups(request);
|
|
|
|
|
|
|
|
List<DeviceGroup> deviceGroups = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
|
|
|
|
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
|
|
|
|
userStoreManager = DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId)
|
|
|
|
UserStoreManager userStoreManager = DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId)
|
|
|
|
.getUserStoreManager();
|
|
|
|
.getUserStoreManager();
|
|
|
|
String[] roleList = userStoreManager.getRoleListOfUser(currentUser);
|
|
|
|
String[] roleList = userStoreManager.getRoleListOfUser(currentUser);
|
|
|
|
int index = 0;
|
|
|
|
List<Integer> groupIds = new ArrayList<>();
|
|
|
|
for (String role : roleList) {
|
|
|
|
for (String role : roleList) {
|
|
|
|
if (role != null && role.contains("Internal/group-")) {
|
|
|
|
if (role != null && role.contains("Internal/group-")) {
|
|
|
|
DeviceGroup deviceGroupBuilder = extractNewGroupFromRole(groups, role);
|
|
|
|
int groupId = Integer.parseInt(role.split("-")[1]);
|
|
|
|
if (deviceGroupBuilder != null
|
|
|
|
if (!groupIds.contains(groupId)) {
|
|
|
|
&& request.getStartIndex() <= index++
|
|
|
|
groupIds.add(groupId);
|
|
|
|
&& index <= request.getRowCount()) {
|
|
|
|
|
|
|
|
groups.put(deviceGroupBuilder.getGroupId(), deviceGroupBuilder);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (DeviceGroup group : allMatchingGroups) {
|
|
|
|
|
|
|
|
int groupId = group.getGroupId();
|
|
|
|
|
|
|
|
if (groupIds.contains(groupId)) {
|
|
|
|
|
|
|
|
if (startIndex <= index && index < count) {
|
|
|
|
|
|
|
|
group.setUsers(this.getUsers(groupId));
|
|
|
|
|
|
|
|
group.setRoles(this.getRoles(groupId));
|
|
|
|
|
|
|
|
deviceGroups.add(group);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
index++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
} catch (UserStoreException e) {
|
|
|
|
} catch (UserStoreException e) {
|
|
|
|
throw new GroupManagementException("Error occurred while getting user store manager.", e);
|
|
|
|
throw new GroupManagementException("Error occurred while getting user store manager.", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new ArrayList<>(groups.values());
|
|
|
|
PaginationResult groupResult = new PaginationResult();
|
|
|
|
|
|
|
|
groupResult.setData(deviceGroups);
|
|
|
|
|
|
|
|
groupResult.setRecordsTotal(index);
|
|
|
|
|
|
|
|
return groupResult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -307,6 +323,20 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private int getGroupCount(GroupPaginationRequest request) throws GroupManagementException {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
|
|
|
|
|
|
|
|
GroupManagementDAOFactory.openConnection();
|
|
|
|
|
|
|
|
return groupDAO.getGroupCount(request, tenantId);
|
|
|
|
|
|
|
|
} catch (GroupManagementDAOException e) {
|
|
|
|
|
|
|
|
throw new GroupManagementException("Error occurred while retrieving all groups in tenant", e);
|
|
|
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
|
|
|
throw new GroupManagementException("Error occurred while opening a connection to the data source.", e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
GroupManagementDAOFactory.closeConnection();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
*/
|
|
|
@ -677,7 +707,7 @@ 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 group = extractNewGroupFromRole(groups, role);
|
|
|
|
DeviceGroup group = checkAndExtractNonExistingGroup(groups, role);
|
|
|
|
if (group != null) {
|
|
|
|
if (group != null) {
|
|
|
|
groups.put(group.getGroupId(), group);
|
|
|
|
groups.put(group.getGroupId(), group);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -708,12 +738,20 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private DeviceGroup extractNewGroupFromRole(Map<Integer, DeviceGroup> groups, String role)
|
|
|
|
/**
|
|
|
|
|
|
|
|
* This method returns group belongs to particular role, if it is not existed in groups map.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param groups existing groups map.
|
|
|
|
|
|
|
|
* @param role group related role which needs to evaluate.
|
|
|
|
|
|
|
|
* @return device group if it is not existing in the groups map.
|
|
|
|
|
|
|
|
* @throws GroupManagementException
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private DeviceGroup checkAndExtractNonExistingGroup(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 buildDeviceGroup(groupId);
|
|
|
|
return getGroup(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);
|
|
|
|