-
Notifications
You must be signed in to change notification settings - Fork 501
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Per Gustavo's code review request, added commands for granting and re…
…voking the superuser status. The commands are used by the dashboard user list page. But not the admin API. Please ask Gustavo if you have any questions about this change. Hopefully this is the last commit for #3614-#3612.
- Loading branch information
Showing
3 changed files
with
133 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/java/edu/harvard/iq/dataverse/engine/command/impl/GrantSuperuserStatusCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* To change this license header, choose License Headers in Project Properties. | ||
* To change this template file, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
package edu.harvard.iq.dataverse.engine.command.impl; | ||
|
||
import edu.harvard.iq.dataverse.Dataset; | ||
import edu.harvard.iq.dataverse.IdServiceBean; | ||
import edu.harvard.iq.dataverse.authorization.Permission; | ||
import edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser; | ||
import edu.harvard.iq.dataverse.engine.command.AbstractVoidCommand; | ||
import edu.harvard.iq.dataverse.engine.command.CommandContext; | ||
import edu.harvard.iq.dataverse.engine.command.DataverseRequest; | ||
import edu.harvard.iq.dataverse.engine.command.RequiredPermissions; | ||
import edu.harvard.iq.dataverse.engine.command.exception.CommandException; | ||
import edu.harvard.iq.dataverse.engine.command.exception.PermissionException; | ||
|
||
/** | ||
* | ||
* @author Leonid Andreev | ||
*/ | ||
// the permission annotation is open, since this is a superuser-only command - | ||
// and that's enforced in the command body: | ||
@RequiredPermissions({}) | ||
public class GrantSuperuserStatusCommand extends AbstractVoidCommand { | ||
|
||
private final AuthenticatedUser targetUser; | ||
|
||
public GrantSuperuserStatusCommand (AuthenticatedUser targetUser, DataverseRequest aRequest) { | ||
super(aRequest, (Dataset)null); | ||
this.targetUser = targetUser; | ||
} | ||
|
||
@Override | ||
protected void executeImpl(CommandContext ctxt) throws CommandException { | ||
|
||
if (!(getUser() instanceof AuthenticatedUser) || !getUser().isSuperuser()) { | ||
throw new PermissionException("Revoke Superuser status command can only be called by superusers.", | ||
this, null, null); | ||
} | ||
|
||
try { | ||
targetUser.setSuperuser(true); | ||
ctxt.em().merge(targetUser); | ||
ctxt.em().flush(); | ||
} catch (Exception e) { | ||
throw new CommandException("Failed to grant the superuser status to user "+targetUser.getIdentifier(), this); | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/edu/harvard/iq/dataverse/engine/command/impl/RevokeSuperuserStatusCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* To change this license header, choose License Headers in Project Properties. | ||
* To change this template file, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
package edu.harvard.iq.dataverse.engine.command.impl; | ||
|
||
import edu.harvard.iq.dataverse.Dataset; | ||
import edu.harvard.iq.dataverse.IdServiceBean; | ||
import edu.harvard.iq.dataverse.authorization.Permission; | ||
import edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser; | ||
import edu.harvard.iq.dataverse.engine.command.AbstractVoidCommand; | ||
import edu.harvard.iq.dataverse.engine.command.CommandContext; | ||
import edu.harvard.iq.dataverse.engine.command.DataverseRequest; | ||
import edu.harvard.iq.dataverse.engine.command.RequiredPermissions; | ||
import edu.harvard.iq.dataverse.engine.command.exception.CommandException; | ||
import edu.harvard.iq.dataverse.engine.command.exception.PermissionException; | ||
|
||
/** | ||
* | ||
* @author Leonid Andreev | ||
*/ | ||
// the permission annotation is open, since this is a superuser-only command - | ||
// and that's enforced in the command body: | ||
@RequiredPermissions({}) | ||
public class RevokeSuperuserStatusCommand extends AbstractVoidCommand { | ||
|
||
private final AuthenticatedUser targetUser; | ||
|
||
public RevokeSuperuserStatusCommand (AuthenticatedUser targetUser, DataverseRequest aRequest) { | ||
super(aRequest, (Dataset)null); | ||
this.targetUser = targetUser; | ||
} | ||
|
||
@Override | ||
protected void executeImpl(CommandContext ctxt) throws CommandException { | ||
|
||
if (!(getUser() instanceof AuthenticatedUser) || !getUser().isSuperuser()) { | ||
throw new PermissionException("Revoke Superuser status command can only be called by superusers.", | ||
this, null, null); | ||
} | ||
|
||
try { | ||
targetUser.setSuperuser(false); | ||
ctxt.em().merge(targetUser); | ||
ctxt.em().flush(); | ||
} catch (Exception e) { | ||
throw new CommandException("Failed to revoke the superuser status for user "+targetUser.getIdentifier(), this); | ||
} | ||
} | ||
|
||
} |