1 | <%-- $Id: edit_role.jsp 7216 2016-10-19 20:17:37Z nicklas $ |
---|
2 | ------------------------------------------------------------------ |
---|
3 | Copyright (C) 2005 Nicklas Nordborg |
---|
4 | Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg, Martin Svensson |
---|
5 | Copyright (C) 2007 Nicklas Nordborg |
---|
6 | |
---|
7 | This file is part of BASE - BioArray Software Environment. |
---|
8 | Available at http://base.thep.lu.se/ |
---|
9 | |
---|
10 | BASE is free software; you can redistribute it and/or |
---|
11 | modify it under the terms of the GNU General Public License |
---|
12 | as published by the Free Software Foundation; either version 3 |
---|
13 | of the License, or (at your option) any later version. |
---|
14 | |
---|
15 | BASE is distributed in the hope that it will be useful, |
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
18 | GNU General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License |
---|
21 | along with BASE. If not, see <http://www.gnu.org/licenses/>. |
---|
22 | ------------------------------------------------------------------ |
---|
23 | |
---|
24 | |
---|
25 | @author Nicklas |
---|
26 | @version 2.0 |
---|
27 | --%> |
---|
28 | <%@ page pageEncoding="UTF-8" session="false" |
---|
29 | import="net.sf.basedb.core.SessionControl" |
---|
30 | import="net.sf.basedb.core.DbControl" |
---|
31 | import="net.sf.basedb.core.SystemItems" |
---|
32 | import="net.sf.basedb.core.Item" |
---|
33 | import="net.sf.basedb.core.ItemContext" |
---|
34 | import="net.sf.basedb.core.Include" |
---|
35 | import="net.sf.basedb.core.Permission" |
---|
36 | import="net.sf.basedb.core.User" |
---|
37 | import="net.sf.basedb.core.Role" |
---|
38 | import="net.sf.basedb.core.RoleKey" |
---|
39 | import="net.sf.basedb.core.ItemQuery" |
---|
40 | import="net.sf.basedb.core.ItemResultList" |
---|
41 | import="net.sf.basedb.core.BaseException" |
---|
42 | import="net.sf.basedb.core.PermissionDeniedException" |
---|
43 | import="net.sf.basedb.core.query.Orders" |
---|
44 | import="net.sf.basedb.core.query.Hql" |
---|
45 | import="net.sf.basedb.clients.web.Base" |
---|
46 | import="net.sf.basedb.clients.web.PermissionUtil" |
---|
47 | import="net.sf.basedb.clients.web.util.HTML" |
---|
48 | import="net.sf.basedb.util.Values" |
---|
49 | import="net.sf.basedb.core.plugin.GuiContext" |
---|
50 | import="net.sf.basedb.clients.web.extensions.ExtensionsControl" |
---|
51 | import="net.sf.basedb.clients.web.extensions.JspContext" |
---|
52 | import="net.sf.basedb.clients.web.extensions.edit.EditUtil" |
---|
53 | import="net.sf.basedb.util.extensions.ExtensionsInvoker" |
---|
54 | import="net.sf.basedb.util.json.JsonUtil" |
---|
55 | import="net.sf.basedb.util.json.NameableConverter" |
---|
56 | import="java.util.EnumSet" |
---|
57 | import="java.util.Set" |
---|
58 | import="java.util.Map" |
---|
59 | import="java.util.List" |
---|
60 | import="org.json.simple.JSONArray" |
---|
61 | import="org.json.simple.JSONObject" |
---|
62 | %> |
---|
63 | <%@ taglib prefix="base" uri="/WEB-INF/base.tld" %> |
---|
64 | <%@ taglib prefix="t" uri="/WEB-INF/tab.tld" %> |
---|
65 | <%@ taglib prefix="ext" uri="/WEB-INF/extensions.tld" %> |
---|
66 | <%! |
---|
67 | private static final int DENY_CODE = PermissionUtil.getPermissionCode(EnumSet.of(Permission.DENIED)); |
---|
68 | private static final int CREATE_CODE = PermissionUtil.getPermissionCode(EnumSet.of(Permission.CREATE)); |
---|
69 | private static final int READ_CODE = PermissionUtil.getPermissionCode(EnumSet.of(Permission.READ)); |
---|
70 | private static final int USE_CODE = PermissionUtil.getPermissionCode(EnumSet.of(Permission.USE)); |
---|
71 | private static final int RESTRICTED_WRITE_CODE = PermissionUtil.getPermissionCode(EnumSet.of(Permission.RESTRICTED_WRITE)); |
---|
72 | private static final int WRITE_CODE = PermissionUtil.getPermissionCode(EnumSet.of(Permission.WRITE)); |
---|
73 | private static final int DELETE_CODE = PermissionUtil.getPermissionCode(EnumSet.of(Permission.DELETE)); |
---|
74 | private static final int SET_OWNER_CODE = PermissionUtil.getPermissionCode(EnumSet.of(Permission.SET_OWNER)); |
---|
75 | private static final int SET_PERMISSION_CODE = PermissionUtil.getPermissionCode(EnumSet.of(Permission.SET_PERMISSION)); |
---|
76 | |
---|
77 | private String getOption(DbControl dc, Role role, Item itemType) |
---|
78 | throws BaseException |
---|
79 | { |
---|
80 | int keyId = SystemItems.getRoleKeyId(itemType); |
---|
81 | if (keyId == 0) return ""; |
---|
82 | |
---|
83 | RoleKey key = RoleKey.getById(dc, keyId); |
---|
84 | Set<Permission> permissions = role == null ? EnumSet.noneOf(Permission.class) : key.getPermissions(role); |
---|
85 | StringBuilder sb = new StringBuilder(); |
---|
86 | sb.append("<option value=\"").append(key.getItemType().name()).append("\">"); |
---|
87 | sb.append(HTML.encodeTags(key.getName())); |
---|
88 | sb.append(" ["); |
---|
89 | if (permissions.contains(Permission.DENIED)) |
---|
90 | { |
---|
91 | sb.append("DENIED"); |
---|
92 | } |
---|
93 | else |
---|
94 | { |
---|
95 | Set<Permission> defined = itemType.getDefinedPermissions(); |
---|
96 | if (defined != null) |
---|
97 | { |
---|
98 | if (defined.contains(Permission.CREATE)) sb.append(permissions.contains(Permission.CREATE) ? "C" : "-"); |
---|
99 | if (defined.contains(Permission.READ)) sb.append(permissions.contains(Permission.READ) ? "R" : "-"); |
---|
100 | if (defined.contains(Permission.USE)) sb.append(permissions.contains(Permission.USE) ? "U" : "-"); |
---|
101 | if (defined.contains(Permission.RESTRICTED_WRITE)) sb.append(permissions.contains(Permission.RESTRICTED_WRITE) ? "A" : "-"); |
---|
102 | if (defined.contains(Permission.WRITE)) sb.append(permissions.contains(Permission.WRITE) ? "W" : "-"); |
---|
103 | if (defined.contains(Permission.DELETE)) sb.append(permissions.contains(Permission.DELETE) ? "D" : "-"); |
---|
104 | if (defined.contains(Permission.SET_OWNER)) sb.append(permissions.contains(Permission.SET_OWNER) ? "O" : "-"); |
---|
105 | if (defined.contains(Permission.SET_PERMISSION)) sb.append(permissions.contains(Permission.SET_PERMISSION) ? "P" : "-"); |
---|
106 | } |
---|
107 | } |
---|
108 | sb.append("]"); |
---|
109 | return sb.toString(); |
---|
110 | } |
---|
111 | %> |
---|
112 | <% |
---|
113 | final Item itemType = Item.ROLE; |
---|
114 | final SessionControl sc = Base.getExistingSessionControl(pageContext, true); |
---|
115 | final ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, null); |
---|
116 | final int itemId = cc.getId(); |
---|
117 | final String ID = sc.getId(); |
---|
118 | final float scale = Base.getScale(sc); |
---|
119 | final DbControl dc = sc.newDbControl(); |
---|
120 | try |
---|
121 | { |
---|
122 | String title = null; |
---|
123 | Role role = null; |
---|
124 | boolean isDefault = false; |
---|
125 | |
---|
126 | if (itemId == 0) |
---|
127 | { |
---|
128 | title = "Create role"; |
---|
129 | cc.removeObject("item"); |
---|
130 | isDefault = Values.getBoolean(cc.getPropertyValue("default"), false); |
---|
131 | } |
---|
132 | else |
---|
133 | { |
---|
134 | role = Role.getById(dc, itemId); |
---|
135 | role.checkPermission(Permission.WRITE); |
---|
136 | cc.setObject("item", role); |
---|
137 | isDefault = role.isDefault(); |
---|
138 | title = "Edit role -- " + HTML.encodeTags(role.getName()); |
---|
139 | |
---|
140 | } |
---|
141 | |
---|
142 | final boolean useUsers = sc.hasPermission(Permission.WRITE, Item.USER); |
---|
143 | RoleKey system = RoleKey.getById(dc, SystemItems.getRoleKeyId(Item.SYSTEM)); |
---|
144 | final boolean hasShareToEveryone = role == null ? false : system.getPermissions(role).contains(Permission.SHARE_TO_EVERYONE); |
---|
145 | final boolean hasActAsAnotherUser = role == null ? false : system.getPermissions(role).contains(Permission.ACT_AS_ANOTHER_USER); |
---|
146 | final boolean hasSelectJobagent = role == null ? false : system.getPermissions(role).contains(Permission.SELECT_JOBAGENT); |
---|
147 | |
---|
148 | // Query to retrieve role keys |
---|
149 | final ItemQuery<RoleKey> roleKeyQuery = RoleKey.getQuery(); |
---|
150 | roleKeyQuery.order(Orders.asc(Hql.property("name"))); |
---|
151 | |
---|
152 | // Load user members as JSON objects |
---|
153 | JSONObject jsonUsers = new JSONObject(); |
---|
154 | jsonUsers.put("itemType", "USER"); |
---|
155 | if (role != null) |
---|
156 | { |
---|
157 | ItemQuery<User> query = role.getUsers(); |
---|
158 | query.include(Include.ALL); |
---|
159 | query.order(Orders.asc(Hql.property("name"))); |
---|
160 | jsonUsers.put("items", JsonUtil.toArray(query.iterate(dc), new NameableConverter())); |
---|
161 | } |
---|
162 | |
---|
163 | JspContext jspContext = ExtensionsControl.createContext(dc, pageContext, GuiContext.item(itemType), role); |
---|
164 | ExtensionsInvoker invoker = EditUtil.useEditExtensions(jspContext); |
---|
165 | %> |
---|
166 | <base:page type="popup" title="<%=title%>" id="edit-page"> |
---|
167 | <base:head scripts="tabcontrol-2.js,linkitems-2.js,~roles.js" styles="tabcontrol.css"> |
---|
168 | <ext:scripts context="<%=jspContext%>" /> |
---|
169 | <ext:stylesheets context="<%=jspContext%>" /> |
---|
170 | </base:head> |
---|
171 | <base:body> |
---|
172 | <h1><%=title%> <base:help tabcontrol="settings" /></h1> |
---|
173 | |
---|
174 | <form action="index.jsp?ID=<%=ID%>" method="post" name="role"> |
---|
175 | <input type="hidden" name="cmd" value="UpdateItem"> |
---|
176 | |
---|
177 | <t:tabcontrol id="settings" |
---|
178 | subclass="content dialogtabcontrol" |
---|
179 | position="bottom" remember="<%=role != null%>" |
---|
180 | extensions="<%=invoker%>"> |
---|
181 | <t:tab id="info" title="Role" helpid="role.edit"> |
---|
182 | <table class="fullform input100"> |
---|
183 | <tr> |
---|
184 | <th>Name</th> |
---|
185 | <td><input class="text required auto-init" data-auto-init="<%=role == null ? "focus-select" : "focus" %>" |
---|
186 | type="text" name="name" |
---|
187 | value="<%=HTML.encodeTags(role == null ? Values.getString(cc.getPropertyValue("name"), "New role") : role.getName())%>" |
---|
188 | maxlength="<%=Role.MAX_NAME_LENGTH%>"></td> |
---|
189 | <td></td> |
---|
190 | </tr> |
---|
191 | <tr class="big"> |
---|
192 | <th>System-level<br>permissions</th> |
---|
193 | <td> |
---|
194 | <input type="checkbox" name="share_to_everyone" id="share_to_everyone" |
---|
195 | value="<%=PermissionUtil.getPermissionCode(EnumSet.of(Permission.SHARE_TO_EVERYONE))%>" |
---|
196 | <%=hasShareToEveryone ? "checked" : ""%> |
---|
197 | ><label for="share_to_everyone">Share to Everyone</label><br> |
---|
198 | <input type="checkbox" name="act_as_another_user" id="act_as_another_user" |
---|
199 | value="<%=PermissionUtil.getPermissionCode(EnumSet.of(Permission.ACT_AS_ANOTHER_USER))%>" |
---|
200 | <%=hasActAsAnotherUser ? "checked" : ""%> |
---|
201 | ><label for="act_as_another_user">Act as another user</label><br> |
---|
202 | <input type="checkbox" name="select_jobagent" id="select_jobagent" |
---|
203 | value="<%=PermissionUtil.getPermissionCode(EnumSet.of(Permission.SELECT_JOBAGENT))%>" |
---|
204 | <%=hasSelectJobagent ? "checked" : ""%> |
---|
205 | ><label for="select_jobagent">Select job agent for jobs</label><br> |
---|
206 | </td> |
---|
207 | <td></td> |
---|
208 | </tr> |
---|
209 | <tr> |
---|
210 | <th>Default</th> |
---|
211 | <td> |
---|
212 | <input type="radio" name="is_default" id="defaultNo" value="0" |
---|
213 | <%=!isDefault ? "checked" : ""%>><label for="defaultNo">no</label> |
---|
214 | <input type="radio" name="is_default" id="defaultYes" value="1" |
---|
215 | <%=isDefault ? "checked" : ""%>><label for="defaultYes">yes</label> |
---|
216 | </td> |
---|
217 | <td></td> |
---|
218 | </tr> |
---|
219 | |
---|
220 | <tr class="dynamic"> |
---|
221 | <th>Description</th> |
---|
222 | <td> |
---|
223 | <textarea class="text" rows="6" name="description" id="description" |
---|
224 | ><%=HTML.encodeTags(role == null ? cc.getPropertyValue("description") : role.getDescription())%></textarea> |
---|
225 | </td> |
---|
226 | <td style="width: 20px;"> |
---|
227 | <base:zoom textarea="description" title="Description" /> |
---|
228 | </td> |
---|
229 | </tr> |
---|
230 | </table> |
---|
231 | </t:tab> |
---|
232 | |
---|
233 | <t:tab id="permissions" title="Permissions" |
---|
234 | tooltip="Set permissions for this role" helpid="role.edit.permissions"> |
---|
235 | <table class="fullform input100"> |
---|
236 | <tr class="dynamic"> |
---|
237 | <th>Item types</th> |
---|
238 | <td> |
---|
239 | <div class="selectionlist"> |
---|
240 | <table> |
---|
241 | <tr> |
---|
242 | <td> |
---|
243 | <select name="items" size="15" id="items" multiple> |
---|
244 | <% |
---|
245 | Map<String, List<Item>> permissionGroups = PermissionUtil.getPermissionGroups(); |
---|
246 | for (Map.Entry<String, List<Item>> entry : permissionGroups.entrySet()) |
---|
247 | { |
---|
248 | String name = entry.getKey(); |
---|
249 | List<Item> items = entry.getValue(); |
---|
250 | %> |
---|
251 | <option class="selectoptionheader" value="">-- <%=name%> |
---|
252 | <% |
---|
253 | for (Item item : items) |
---|
254 | { |
---|
255 | %> |
---|
256 | <%=getOption(dc, role, item)%> |
---|
257 | <% |
---|
258 | } |
---|
259 | } |
---|
260 | %> |
---|
261 | </select> |
---|
262 | </td> |
---|
263 | <td style="vertical-align: top;"> |
---|
264 | <div style="width: 15em;"> |
---|
265 | <b>Permissions</b><br> |
---|
266 | <input type="checkbox" name="deny" id="deny" value="<%=DENY_CODE%>"><label for="deny">Deny</label><br> |
---|
267 | <input type="checkbox" name="create" id="create" value="<%=CREATE_CODE%>"><label for="create">Create</label><br> |
---|
268 | <input type="checkbox" name="read" id="read" value="<%=READ_CODE%>"><label for="read">Read</label><br> |
---|
269 | <input type="checkbox" name="use" id="use" value="<%=USE_CODE%>"><label for="use">Use</label><br> |
---|
270 | <input type="checkbox" name="restricted_write" id="restricted_write" value="<%=RESTRICTED_WRITE_CODE%>"><label for="restricted_write">Annotate</label><br> |
---|
271 | <input type="checkbox" name="write" id="write" value="<%=WRITE_CODE%>"><label for="write">Write</label><br> |
---|
272 | <input type="checkbox" name="delete" id="delete" value="<%=DELETE_CODE%>"><label for="delete">Delete</label><br> |
---|
273 | <input type="checkbox" name="set_owner" id="set_owner" value="<%=SET_OWNER_CODE%>"><label for="set_owner">Set owner</label><br> |
---|
274 | <input type="checkbox" name="set_permission" id="set_permission" value="<%=SET_PERMISSION_CODE%>"><label for="set_permission">Set permission</label><br> |
---|
275 | </div> |
---|
276 | </td> |
---|
277 | </tr> |
---|
278 | </table> |
---|
279 | </div> |
---|
280 | </td> |
---|
281 | </tr> |
---|
282 | </table> |
---|
283 | </t:tab> |
---|
284 | |
---|
285 | <t:tab id="members" title="Members" tooltip="Add/remove members of this role" |
---|
286 | helpid="role.edit.members"> |
---|
287 | <table class="fullform input100"> |
---|
288 | <tr class="dynamic"> |
---|
289 | <th>Members</th> |
---|
290 | <td> |
---|
291 | <div class="selectionlist"> |
---|
292 | <table> |
---|
293 | <tr> |
---|
294 | <td> |
---|
295 | <select name="members" id="members" |
---|
296 | class="auto-init" data-auto-init="link-container" |
---|
297 | data-initial-items="[<%=HTML.encodeTags(jsonUsers.toJSONString()) %>]" |
---|
298 | data-initial-action="<%=role == null ? 0 : 1%>" |
---|
299 | size="15" multiple <%=!useUsers ? "disabled readonly class=\"disabled\"" : ""%>> |
---|
300 | </select> |
---|
301 | </td> |
---|
302 | <td style="vertical-align: top;"> |
---|
303 | <base:buttongroup vertical="true"> |
---|
304 | <base:button |
---|
305 | id="btnAddUsers" |
---|
306 | subclass="leftaligned auto-init" |
---|
307 | data-auto-init="add-link" |
---|
308 | data-list-id="members" |
---|
309 | data-item-type="USER" |
---|
310 | style="width: 12em;" |
---|
311 | title="Add users…" |
---|
312 | tooltip="Add users to this role" |
---|
313 | disabled="<%=!useUsers %>" |
---|
314 | /> |
---|
315 | <base:button |
---|
316 | id="btnRemoveUsers" |
---|
317 | subclass="leftaligned auto-init" |
---|
318 | data-auto-init="remove-link" |
---|
319 | data-list-id="members" |
---|
320 | style="width: 12em;" |
---|
321 | title="Remove" |
---|
322 | tooltip="Remove the selected items from this role" |
---|
323 | disabled="<%=!useUsers%>" |
---|
324 | /> |
---|
325 | </base:buttongroup> |
---|
326 | </td> |
---|
327 | </tr> |
---|
328 | </table> |
---|
329 | </div> |
---|
330 | </td> |
---|
331 | </tr> |
---|
332 | </table> |
---|
333 | </t:tab> |
---|
334 | </t:tabcontrol> |
---|
335 | <% |
---|
336 | ItemResultList<RoleKey> roleKeys = roleKeyQuery.list(dc); |
---|
337 | for (RoleKey key : roleKeys) |
---|
338 | { |
---|
339 | int permissionCode = role == null ? 0 : PermissionUtil.getPermissionCode(key.getPermissions(role)); |
---|
340 | int definedCode = key.getItemType().getDefinedPermissions() == null ? 0 : PermissionUtil.getPermissionCode(key.getItemType().getDefinedPermissions()); |
---|
341 | String name = key.getItemType().name(); |
---|
342 | %> |
---|
343 | <input type="hidden" name="<%=name%>" value="<%=permissionCode%>"> |
---|
344 | <input type="hidden" name="<%=name%>_defined" value="<%=definedCode%>"> |
---|
345 | <% |
---|
346 | } |
---|
347 | %> |
---|
348 | </form> |
---|
349 | |
---|
350 | <div class="legend"> |
---|
351 | <base:icon image="required.png" />= required information |
---|
352 | </div> |
---|
353 | |
---|
354 | <base:buttongroup subclass="dialogbuttons"> |
---|
355 | <base:button id="btnSave" title="Save" /> |
---|
356 | <base:button id="close" title="Cancel" /> |
---|
357 | </base:buttongroup> |
---|
358 | |
---|
359 | </base:body> |
---|
360 | </base:page> |
---|
361 | <% |
---|
362 | } |
---|
363 | finally |
---|
364 | { |
---|
365 | if (dc != null) dc.close(); |
---|
366 | } |
---|
367 | %> |
---|