Skip to content

Commit ede28bd

Browse files
committed
[KNOWAGE-9272] Add support to retrieve users and roles across all tenants
1 parent 7e2622e commit ede28bd

3 files changed

Lines changed: 91 additions & 36 deletions

File tree

knowage-core/src/main/java/it/eng/spagobi/api/v2/UserResource.java

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,14 @@ public Response insertUser(@Valid UserBO requestDTO) {
176176
LOGGER.error("public is reserved prefix for user id");
177177
throw new SpagoBIServiceException("SPAGOBI_SERVICE", "public_ is a reserved prefix for user name", null);
178178
}
179-
ISbiUserDAO usersDao = null;
179+
ISbiUserDAO usersDao = DAOFactory.getSbiUserDAO();
180180

181-
usersDao = DAOFactory.getSbiUserDAO();
182-
checkIfUserCanBeAdded(requestDTO, usersDao);
181+
boolean isAdmin = userRequestDtoIsAdmin(requestDTO);
182+
183+
if (!userCanBeAdded(requestDTO, usersDao, isAdmin)) {
184+
LOGGER.error("The limit for creating {} users has been reached.", isAdmin ? "admin " : "end ");
185+
throw new SpagoBIServiceException("Create user", "The limit for creating " + (isAdmin ? "admin " : "end ") + "users has been reached.");
186+
}
183187

184188
usersDao.setUserProfile(getUserProfile());
185189
SbiUser existingUser = usersDao.loadSbiUserByUserId(userId);
@@ -252,44 +256,39 @@ public Response insertUser(@Valid UserBO requestDTO) {
252256
}
253257
}
254258

255-
private void checkIfUserCanBeAdded(UserBO requestDTO, ISbiUserDAO usersDao) {
256-
List<UserBO> dbUsers = usersDao.loadUsers();
259+
private boolean userCanBeAdded(UserBO requestDTO, ISbiUserDAO usersDao, boolean isAdmin) {
260+
List<SbiUser> dbUsers = usersDao.loadAllTenantsUsers();
257261

258-
boolean isAdmin = userRequestDtoIsAdmin(requestDTO);
262+
List<SbiUser> usersToCheck = filterUsersToCheck(dbUsers, isAdmin);
263+
return ProductProfiler.canAddAUser(usersToCheck.size(), isAdmin);
264+
}
259265

260-
List<UserBO> usersToCheck = filterUsersToCheck(dbUsers, isAdmin);
261-
if (!ProductProfiler.canAddAUser(usersToCheck.size(), isAdmin)) {
262-
LOGGER.error("Maximum number of {} users reached", isAdmin ? "admin" : "end");
263-
throw new SpagoBIRestServiceException("Maximum number of " + (isAdmin ? "admin" : "end") + " users reached", buildLocaleFromSession(),
264-
new Throwable());
265-
}
266+
private List<SbiUser> filterUsersToCheck(List<SbiUser> sbiUsers, boolean isAdmin) {
267+
ISbiUserDAO usersDao = DAOFactory.getSbiUserDAO();
268+
usersDao.setUserProfile(getUserProfile());
269+
270+
return filterUsersWithRoles(sbiUsers, isAdmin, usersDao);
271+
}
272+
273+
private List<SbiUser> filterUsersWithRoles(List<SbiUser> sbiUsers, boolean isAdmin, ISbiUserDAO usersDao) {
274+
return sbiUsers.stream()
275+
.filter(user -> hasApplicableRoles(user, isAdmin, usersDao))
276+
.toList();
266277
}
267278

268-
private List<UserBO> filterUsersToCheck(List<UserBO> sbiUsers, boolean isAdmin) {
269-
return sbiUsers.stream()
270-
.filter(user -> {
271-
try {
272-
IRoleDAO rolesDao = DAOFactory.getRoleDAO();
273-
rolesDao.setUserProfile(getUserProfile());
274-
return user.getSbiExtUserRoleses().stream()
275-
.anyMatch(roleId -> {
276-
try {
277-
SbiExtRoles role = rolesDao.loadSbiExtRoleById((Integer) roleId);
278-
return role != null &&
279-
isRoleApplicable(role, isAdmin);
280-
} catch (Exception e) {
281-
LOGGER.error("Error loading role with id: {}", roleId, e);
282-
return false;
283-
}
284-
});
285-
} catch (Exception e) {
286-
LOGGER.error("Error accessing roles DAO", e);
287-
return false;
288-
}
289-
})
290-
.toList();
279+
private boolean hasApplicableRoles(SbiUser user, boolean isAdmin, ISbiUserDAO usersDao) {
280+
try {
281+
ArrayList<SbiExtRoles> userRoles = usersDao.loadSbiUserRolesByIdAllTenants(user.getId());
282+
283+
return userRoles.stream()
284+
.anyMatch(role -> role != null && isRoleApplicable(role, isAdmin));
285+
} catch (Exception e) {
286+
LOGGER.error("Error loading roles for user with id: {}", user.getId(), e);
287+
return false;
288+
}
291289
}
292290

291+
293292
private boolean userRequestDtoIsAdmin(@Valid UserBO requestDTO) {
294293
List<Integer> sbiExtUserRoleses = requestDTO.getSbiExtUserRoleses();
295294
IRoleDAO rolesDao = DAOFactory.getRoleDAO();
@@ -339,8 +338,12 @@ public Response updateUser(@PathParam("id") Integer id, @Valid UserBO requestDTO
339338
}
340339

341340
usersDao = DAOFactory.getSbiUserDAO();
341+
boolean isAdmin = userRequestDtoIsAdmin(requestDTO);
342342

343-
checkIfUserCanBeAdded(requestDTO, usersDao);
343+
if (!userCanBeAdded(requestDTO, usersDao, isAdmin)) {
344+
LOGGER.error("The limit for creating {} users has been reached.", isAdmin ? "admin " : "end ");
345+
throw new SpagoBIServiceException("Update user", "The limit for creating " + (isAdmin ? "admin " : "end ") + "users has been reached.");
346+
}
344347

345348
SbiUser sbiUser = new SbiUser();
346349
sbiUser.changeId(id);

knowagedao/src/main/java/it/eng/spagobi/profiling/dao/ISbiUserDAO.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public interface ISbiUserDAO extends ISpagoBIDao, EmittingEventDAO<UserEventsEme
5050

5151
ArrayList<SbiUser> loadSbiUsers();
5252

53+
ArrayList<SbiUser> loadAllTenantsUsers();
54+
5355
ArrayList<UserBO> loadUsers();
5456

5557
List<UserBO> loadUsers(QueryFilters filters);
@@ -88,4 +90,5 @@ public interface ISbiUserDAO extends ISpagoBIDao, EmittingEventDAO<UserEventsEme
8890

8991
void resetOtpSecret(Integer userId);
9092

93+
ArrayList<SbiExtRoles> loadSbiUserRolesByIdAllTenants(int id);
9194
}

knowagedao/src/main/java/it/eng/spagobi/profiling/dao/SbiUserDAOHibImpl.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,31 @@ public void resetOtpSecret(Integer userId) {
229229
}
230230
}
231231

232+
@Override
233+
public ArrayList<SbiExtRoles> loadSbiUserRolesByIdAllTenants(int id) {
234+
LOGGER.debug("IN");
235+
236+
Session aSession = null;
237+
Transaction tx = null;
238+
try {
239+
aSession = getSession();
240+
disableTenantFilter(aSession);
241+
tx = aSession.beginTransaction();
242+
String q = "select us.sbiExtUserRoleses from SbiUser us where us.id = :id";
243+
Query query = aSession.createQuery(q);
244+
query.setInteger("id", id);
245+
246+
ArrayList<SbiExtRoles> result = (ArrayList<SbiExtRoles>) query.list();
247+
return result;
248+
} catch (HibernateException he) {
249+
rollbackIfActive(tx);
250+
throw new SpagoBIDAOException("Error while loading user role with id " + id, he);
251+
} finally {
252+
LOGGER.debug("OUT");
253+
closeSessionIfOpen(aSession);
254+
}
255+
}
256+
232257
/**
233258
* Insert SbiUser
234259
*
@@ -423,6 +448,30 @@ public ArrayList<SbiUser> loadSbiUsers() {
423448
}
424449
}
425450

451+
@Override
452+
public ArrayList<SbiUser> loadAllTenantsUsers() {
453+
LOGGER.debug("IN");
454+
455+
Session aSession = null;
456+
Transaction tx = null;
457+
try {
458+
aSession = getSession();
459+
this.disableTenantFilter(aSession);
460+
tx = aSession.beginTransaction();
461+
String q = "from SbiUser ";
462+
Query query = aSession.createQuery(q);
463+
464+
ArrayList<SbiUser> result = (ArrayList<SbiUser>) query.list();
465+
return result;
466+
} catch (HibernateException he) {
467+
rollbackIfActive(tx);
468+
throw new SpagoBIDAOException("Error while loading users", he);
469+
} finally {
470+
LOGGER.debug("OUT");
471+
closeSessionIfOpen(aSession);
472+
}
473+
}
474+
426475
@Override
427476
public Integer fullSaveOrUpdateSbiUser(SbiUser user) {
428477
LOGGER.debug("IN");

0 commit comments

Comments
 (0)