Skip to content

Commit

Permalink
Changes to Short URL admin page (#4813)
Browse files Browse the repository at this point in the history
* Display short URLs in a LabKey grid.

* Code review changes:
- Display ShortURL table only if container is root and user has AdminOperationsPermission
- Enable standard update and delete
  • Loading branch information
vagisha authored Oct 11, 2023
1 parent 65f562c commit 372298d
Show file tree
Hide file tree
Showing 5 changed files with 293 additions and 86 deletions.
82 changes: 56 additions & 26 deletions core/src/org/labkey/core/admin/AdminController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9463,39 +9463,14 @@ public boolean isDelete()
{
return _delete;
}

public List<ShortURLRecord> getSavedShortURLs()
{
return _savedShortURLs;
}

public void setSavedShortURLs(List<ShortURLRecord> savedShortURLs)
{
_savedShortURLs = savedShortURLs;
}
}

@AdminConsoleAction
@RequiresPermission(AdminPermission.class)
public class ShortURLAdminAction extends FormViewAction<ShortURLForm>
public abstract class AbstractShortURLAdminAction extends FormViewAction<ShortURLForm>
{
@Override
public void validateCommand(ShortURLForm target, Errors errors) {}

@Override
public ModelAndView getView(ShortURLForm form, boolean reshow, BindException errors)
{
form.setSavedShortURLs(ShortURLService.get().getAllShortURLs());
JspView<ShortURLForm> newView = new JspView<>("/org/labkey/core/admin/createNewShortURL.jsp", form, errors);
newView.setTitle("Create New Short URL");
newView.setFrame(WebPartView.FrameType.PORTAL);
JspView<ShortURLForm> existingView = new JspView<>("/org/labkey/core/admin/existingShortURLs.jsp", form, errors);
existingView.setTitle("Existing Short URLs");
existingView.setFrame(WebPartView.FrameType.PORTAL);

return new VBox(newView, existingView);
}

@Override
public boolean handlePost(ShortURLForm form, BindException errors) throws Exception
{
Expand Down Expand Up @@ -9572,6 +9547,27 @@ public boolean handlePost(ShortURLForm form, BindException errors) throws Except
}
return true;
}
}

@AdminConsoleAction
@RequiresPermission(AdminPermission.class)
public class ShortURLAdminAction extends AbstractShortURLAdminAction
{
@Override
public ModelAndView getView(ShortURLForm form, boolean reshow, BindException errors)
{
JspView<ShortURLForm> newView = new JspView<>("/org/labkey/core/admin/createNewShortURL.jsp", form, errors);
newView.setTitle("Create New Short URL");
newView.setFrame(WebPartView.FrameType.PORTAL);

QuerySettings qSettings = new QuerySettings(getViewContext(), "ShortURL", "ShortURL");
qSettings.setBaseSort(new Sort("-Created"));
QueryView existingView = new QueryView(new CoreQuerySchema(getUser(), getContainer()), qSettings, errors);
existingView.setTitle("Existing Short URLs");
existingView.setFrame(WebPartView.FrameType.PORTAL);

return new VBox(newView, existingView);
}

@Override
public URLHelper getSuccessURL(ShortURLForm form)
Expand All @@ -9587,6 +9583,40 @@ public void addNavTrail(NavTree root)
}
}

@RequiresPermission(AdminOperationsPermission.class)
public class UpdateShortURLAction extends AbstractShortURLAdminAction
{
@Override
public ModelAndView getView(ShortURLForm form, boolean reshow, BindException errors)
{
var shortUrlRecord = ShortURLService.get().resolveShortURL(form.getShortURL());
if (shortUrlRecord == null)
{
errors.addError(new LabKeyError("Short URL does not exist: " + form.getShortURL()));
return new SimpleErrorView(errors);
}
form.setFullURL(shortUrlRecord.getFullURL());

JspView<ShortURLForm> view = new JspView<>("/org/labkey/core/admin/updateShortURL.jsp", form, errors);
view.setTitle("Update Short URL");
view.setFrame(WebPartView.FrameType.PORTAL);
return view;
}

@Override
public URLHelper getSuccessURL(ShortURLForm form)
{
return new ActionURL(ShortURLAdminAction.class, getContainer());
}

@Override
public void addNavTrail(NavTree root)
{
setHelpTopic("shortURL");
addAdminNavTrail(root, "Update Short URL", getClass());
}
}

// API for reporting client-side exceptions.
// UNDONE: Throttle by IP to avoid DOS from buggy clients.
@Marshal(Marshaller.Jackson)
Expand Down
60 changes: 0 additions & 60 deletions core/src/org/labkey/core/admin/existingShortURLs.jsp

This file was deleted.

37 changes: 37 additions & 0 deletions core/src/org/labkey/core/admin/updateShortURL.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<%@ page import="org.labkey.api.view.HttpView" %>
<%@ page import="org.labkey.core.admin.AdminController" %>
<%@ page import="org.labkey.api.view.ActionURL" %>
<%@ page extends="org.labkey.api.jsp.JspBase" %>
<%@ taglib prefix="labkey" uri="http://www.labkey.org/taglib" %>

<labkey:errors/>

<%
AdminController.ShortURLForm bean = (AdminController.ShortURLForm) HttpView.currentModel();
%>

<labkey:form action="<%=urlFor(AdminController.UpdateShortURLAction.class)%>" method="POST" id="updateShortUrlForm">
<table class="lk-fields-table">
<tr>
<td class="labkey-form-label">Short URL: </td>
<td>
<input type="hidden" name="shortURL" value="<%= h(bean.getShortURL()) %>"/>
<%= h(bean.getShortURL()) %>
</td>
</tr>
<tr>
<td class="labkey-form-label">Target URL: </td>
<td><textarea rows="3" cols="80" name="fullURL"><%= h(bean.getFullURL()) %></textarea></td>
</tr>
</table>
<div style="margin-top: 10px;">
<%= button("Update").submit(true) %>
<%= button("Cancel").href(new ActionURL(AdminController.ShortURLAdminAction.class, getContainer())) %>
</div>
</labkey:form>

<div style="margin-top: 20px;">
<%= button("Delete")
.usePost("Are you sure you want to delete the short URL " + bean.getShortURL() + "?")
.href(urlFor(AdminController.UpdateShortURLAction.class).addParameter("shortURL", bean.getShortURL()).addParameter("delete", true)) %>
</div>
4 changes: 4 additions & 0 deletions core/src/org/labkey/core/query/CoreQuerySchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public class CoreQuerySchema extends UserSchema
public static final String USERS_MSG_SETTINGS_TABLE_NAME = "UsersMsgPrefs";
public static final String SCHEMA_DESCR = "Contains data about the system users and groups.";
public static final String VIEW_CATEGORY_TABLE_NAME = "ViewCategory";
public static final String SHORTURL_TABLE_NAME = "ShortURL";

public CoreQuerySchema(User user, Container c)
{
Expand Down Expand Up @@ -177,6 +178,9 @@ public TableInfo createTable(String name, ContainerFilter cf)
return new ViewCategoryTable(ViewCategoryManager.getInstance().getTableInfoCategories(), this, cf);
if (MISSING_VALUE_INDICATOR_TABLE_NAME.equalsIgnoreCase(name))
return getMVIndicatorTable(cf);
if (SHORTURL_TABLE_NAME.equalsIgnoreCase(name) && ShortUrlTableInfo.canDisplayTable(getUser(), getContainer()))
return new ShortUrlTableInfo(this);

return null;
}

Expand Down
Loading

0 comments on commit 372298d

Please sign in to comment.