@Override
@Transactional
public void grantRoleToUserOnProject(AuthenticatedProject authenticatedProject, long granteeUserId, long roleId)
throws CloudException {
RoleData role = authRepository.getRoles().find(roleId);
if (role == null) {
throw new IllegalArgumentException("Cannot find role");
}
long projectId = authenticatedProject.getProjectId();
UserData granteeData = authRepository.getUsers().find(granteeUserId);
if (granteeData == null) {
throw new IllegalArgumentException();
}
UserData.Builder b = UserData.newBuilder(granteeData);
{
ProjectRoles.Builder pb = null;
for (ProjectRoles.Builder i : b.getProjectRolesBuilderList()) {
if (i.getProject() == projectId) {
pb = i;
break;
}
}
if (pb == null) {
pb = b.addProjectRolesBuilder();
pb.setProject(projectId);
}
if (!pb.hasSecretData()) {
try {
pb.setSecretData(Secrets.buildProjectRolesSecret(granteeData, authenticatedProject));
} catch (KeyczarException e) {
throw new CloudException("Crypto error granting project role", e);
}
}
if (!pb.getRoleList().contains(role.getId())) {
pb.addRole(role.getId());
}
authRepository.getUsers().update(b);
}
}