Skip to content

Commit

Permalink
Features/app permissions (#135)
Browse files Browse the repository at this point in the history
* Allow to assign an app multiple groups

* Fix html support in signup dialog

---------

Co-authored-by: Lukas Forer <lukas.forer@i-med.ac.at>
  • Loading branch information
seppinho and lukfor authored Dec 18, 2023
1 parent d00598e commit 0134ab7
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 45 deletions.
64 changes: 36 additions & 28 deletions src/main/html/webapp/components/admin/app/list/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,40 +228,48 @@ export default Control.extend({

Group.findAll({},
function(groups) {
var selection = new canMap();
selection.attr('group', application.attr('permission'));
selection.attr('name', '');

bootbox.confirm(templatePermission({
selection: selection,
application: application,
groups: groups
}),

var roles = application.attr('permission').split(',');

var options = '';
groups.forEach(function(group, index) {
if ($.inArray(group.attr('name'), roles) >= 0) {
options = options + '<label class="checkbox"><input type="checkbox" name="role-select" value="' + group.attr('name') + '" checked />';
//options = options + '<option selected>' + group.attr('name') + '</option>';
} else {
//options = options + '<option>' + group.attr('name') + '</option>';
options = options + '<label class="checkbox"><input type="checkbox" name="role-select" value="' + group.attr('name') + '" />';
}
options = options + ' <b>' + group.attr('name') + '</b></label><br>';
});

bootbox.confirm(
'<h4>Edit permission of ' + application.attr('name') + '</h4><hr><form id="role-form">' + options + '</form>',
function(result) {
if (result) {
var group = selection.attr('group');
if (group !== '') {
application.attr('permission', group);
application.save(function(data) {},
function(response) {
showErrorDialog("Operation failed", response);
});
} else {
var name = selection.attr('name');
if (name !== '') {
application.attr('permission', name);
application.save(function(data) {},
function(response) {
showErrorDialog("Operation failed", response);
});

} else {
bootbox.alert("Error: Please enter a name for the new group.")

var boxes = $('#role-form input:checkbox');
var checked = [];
for (var i = 0; boxes[i]; ++i) {
if (boxes[i].checked) {
checked.push(boxes[i].value);
}
}

var text = checked.join(',');
application.attr('permission', text);
application.save(function(data) {},
function(response) {
showErrorDialog("Operation failed", response);
});

}
});
}
);

},
function(response) {
new ErrorPage(element, response);
});

},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<input type="radio" id="anonymous1" name="anonymous" value="0" checked> <label for="anonymous1" class="control-label">E-Mail Address</label>
<div class="form-group" style="margin-left: 30px;">
<p>
{{userEmailDescription}}
{{{userEmailDescription}}}
</p>
<label for="mail" class="control-label">E-Mail:</label>
<input id="mail" name="mail" type="text" class="form-control col-sm-3" autocomplete="off">
Expand All @@ -48,7 +48,7 @@
<input type="radio" id="anonymous2" name="anonymous" value="1"> <label for="anonymous2" class="control-label">I don't want to provide my email address</label>
<div class="form-group" style="margin-left: 30px;">
<p>
{{userWithoutEmailDescription}}
{{{userWithoutEmailDescription}}}
</p>
</div>
</div>
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/cloudgene/mapred/api/v2/admin/GetGroups.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;
import java.util.Vector;

import cloudgene.mapred.api.v2.users.RegisterUser;
import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
Expand Down Expand Up @@ -36,18 +37,21 @@ public Representation get() {
List<Group> groups = new Vector<Group>();
groups.add(new Group("admin"));
groups.add(new Group("user"));
groups.add(new Group(RegisterUser.DEFAULT_ANONYMOUS_ROLE.toLowerCase()));

ApplicationRepository repository = getApplicationRepository();

for (Application application : repository.getAll()) {
Group group = new Group(application.getPermission());
if (!groups.contains(group)) {
group.addApp(application.getId());
groups.add(group);
} else {
int index = groups.indexOf(group);
group = groups.get(index);
group.addApp(application.getId());
for (String permission: application.getPermissions()) {
Group group = new Group(permission);
if (!groups.contains(group)) {
group.addApp(application.getId());
groups.add(group);
} else {
int index = groups.indexOf(group);
group = groups.get(index);
group.addApp(application.getId());
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/cloudgene/mapred/api/v2/server/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,11 @@ public Representation updateApp(Representation entity) {
application.checkForChanges();

JSONObject jsonObject = JSONConverter.convert(application);
updateState(application, jsonObject);
try {
updateState(application, jsonObject);
}catch (Error e ){
e.printStackTrace();
}

// read config
Map<String, String> config = repository.getConfig(wdlApp);
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/cloudgene/mapred/apps/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ public String getPermission() {
return permission;
}

public String[] getPermissions() {
return permission.split(",");
}

public boolean hasPermission(String group) {
String[] permissions = getPermissions();
for (String permission : permissions) {
if (permission.toLowerCase().equals(group.toLowerCase())) {
return true;
}
}
return false;
}


public void setPermission(String permission) {
this.permission = permission;
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/cloudgene/mapred/apps/ApplicationRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public Application getByIdAndUser(String id, User user) {
if (app != null && app.isEnabled() && app.isLoaded() && !app.hasSyntaxError()) {

if (user == null) {
if (app.getPermission().toLowerCase().equals("public")) {
if (app.hasPermission(("public"))) {
if (app.getWdlApp().getWorkflow() != null) {
return app;
} else {
Expand All @@ -98,8 +98,8 @@ public Application getByIdAndUser(String id, User user) {
}
}

if (user.isAdmin() || user.hasRole(app.getPermission())
|| app.getPermission().toLowerCase().equals("public")) {
if (user.isAdmin() || user.hasRole(app.getPermissions())
|| app.hasPermission("public")) {
if (app.getWdlApp().getWorkflow() != null) {
return app;
} else {
Expand Down Expand Up @@ -175,16 +175,16 @@ public List<WdlApp> getAllByUser(User user, boolean appsOnly) {
boolean using = true;

if (user == null) {
if (application.getPermission().toLowerCase().equals("public")) {
if (application.hasPermission("public")) {
using = true;
} else {
using = false;
}
} else {

if (!user.isAdmin() && !application.getPermission().toLowerCase().equals("public")) {
if (!user.isAdmin() && !application.hasPermission("public")) {

if (!user.hasRole(application.getPermission())) {
if (!user.hasRole(application.getPermissions())) {
using = false;
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/cloudgene/mapred/core/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ public boolean hasRole(String role) {
return false;
}

public boolean hasRole(String[] roles) {
if (this.roles == null || roles == null) {
return false;
}
for (int i = 0; i < roles.length; i++) {
if (hasRole(roles[i])) {
return true;
}
}
return false;
}

public boolean isAdmin() {
return hasRole(ROLE_ADMIN);
}
Expand Down

0 comments on commit 0134ab7

Please sign in to comment.