c834450d04657fbfaf6b7d5bbb151a9bee060359
[public/netxms.git] / webui / webapp / ObjectManager / src / org / netxms / ui / eclipse / objectmanager / propertypages / AccessControl.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2014 Victor Kirhenshtein
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 package org.netxms.ui.eclipse.objectmanager.propertypages;
20
21 import java.util.HashMap;
22 import java.util.Iterator;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.core.runtime.Platform;
25 import org.eclipse.jface.viewers.ArrayContentProvider;
26 import org.eclipse.jface.viewers.ISelectionChangedListener;
27 import org.eclipse.jface.viewers.IStructuredSelection;
28 import org.eclipse.jface.viewers.SelectionChangedEvent;
29 import org.eclipse.jface.window.Window;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.events.SelectionEvent;
32 import org.eclipse.swt.events.SelectionListener;
33 import org.eclipse.swt.layout.FillLayout;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.layout.RowLayout;
37 import org.eclipse.swt.widgets.Button;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Control;
40 import org.eclipse.swt.widgets.Group;
41 import org.eclipse.ui.dialogs.PropertyPage;
42 import org.netxms.client.AccessListElement;
43 import org.netxms.client.NXCObjectModificationData;
44 import org.netxms.client.NXCSession;
45 import org.netxms.client.constants.UserAccessRights;
46 import org.netxms.client.objects.AbstractObject;
47 import org.netxms.client.users.AbstractUserObject;
48 import org.netxms.ui.eclipse.jobs.ConsoleJob;
49 import org.netxms.ui.eclipse.objectmanager.Activator;
50 import org.netxms.ui.eclipse.objectmanager.Messages;
51 import org.netxms.ui.eclipse.objectmanager.propertypages.helpers.AccessListComparator;
52 import org.netxms.ui.eclipse.objectmanager.propertypages.helpers.AccessListLabelProvider;
53 import org.netxms.ui.eclipse.shared.ConsoleSharedData;
54 import org.netxms.ui.eclipse.tools.WidgetHelper;
55 import org.netxms.ui.eclipse.usermanager.dialogs.SelectUserDialog;
56 import org.netxms.ui.eclipse.widgets.SortableTableViewer;
57
58 /**
59 * Object's "access control" property page
60 */
61 public class AccessControl extends PropertyPage
62 {
63 private AbstractObject object;
64 private SortableTableViewer userList;
65 private HashMap<Integer, Button> accessChecks = new HashMap<Integer, Button>(11);
66 private HashMap<Long, AccessListElement> acl;
67 private Button checkInherit;
68
69 /* (non-Javadoc)
70 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
71 */
72 @Override
73 protected Control createContents(Composite parent)
74 {
75 object = (AbstractObject)getElement().getAdapter(AbstractObject.class);
76
77 AccessListElement[] origAcl = object.getAccessList();
78 acl = new HashMap<Long, AccessListElement>(origAcl.length);
79 for(int i = 0; i < origAcl.length; i++)
80 acl.put(origAcl[i].getUserId(), new AccessListElement(origAcl[i]));
81
82 // Initiate loading of user manager plugin if it was not loaded before
83 Platform.getAdapterManager().loadAdapter(new AccessListElement(0, 0), "org.eclipse.ui.model.IWorkbenchAdapter"); //$NON-NLS-1$
84
85 Composite dialogArea = new Composite(parent, SWT.NONE);
86
87 GridLayout layout = new GridLayout();
88 layout.marginHeight = 0;
89 layout.marginWidth = 0;
90 layout.numColumns = 2;
91 dialogArea.setLayout(layout);
92
93 Group users = new Group(dialogArea, SWT.NONE);
94 users.setText(Messages.get().AccessControl_UsersGroups);
95 GridData gd = new GridData();
96 gd.grabExcessHorizontalSpace = true;
97 gd.grabExcessVerticalSpace = true;
98 gd.horizontalAlignment = SWT.FILL;
99 gd.verticalAlignment = SWT.FILL;
100 users.setLayoutData(gd);
101
102 layout = new GridLayout();
103 users.setLayout(layout);
104
105 final String[] columnNames = { Messages.get().AccessControl_ColLogin, Messages.get().AccessControl_ColRights };
106 final int[] columnWidths = { 150, 100 };
107 userList = new SortableTableViewer(users, columnNames, columnWidths, 0, SWT.UP,
108 SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
109 userList.setContentProvider(new ArrayContentProvider());
110 userList.setLabelProvider(new AccessListLabelProvider());
111 userList.setComparator(new AccessListComparator());
112 userList.setInput(acl.values().toArray());
113 gd = new GridData();
114 gd.grabExcessHorizontalSpace = true;
115 gd.grabExcessVerticalSpace = true;
116 gd.horizontalAlignment = SWT.FILL;
117 gd.verticalAlignment = SWT.FILL;
118 userList.getControl().setLayoutData(gd);
119
120 Composite buttons = new Composite(users, SWT.NONE);
121 FillLayout buttonsLayout = new FillLayout();
122 buttonsLayout.spacing = WidgetHelper.INNER_SPACING;
123 buttons.setLayout(buttonsLayout);
124 gd = new GridData();
125 gd.horizontalAlignment = SWT.RIGHT;
126 gd.widthHint = 184;
127 buttons.setLayoutData(gd);
128
129 final Button addButton = new Button(buttons, SWT.PUSH);
130 addButton.setText(Messages.get().AccessControl_Add);
131 addButton.addSelectionListener(new SelectionListener() {
132 @Override
133 public void widgetDefaultSelected(SelectionEvent e)
134 {
135 widgetSelected(e);
136 }
137
138 @Override
139 public void widgetSelected(SelectionEvent e)
140 {
141 SelectUserDialog dlg = new SelectUserDialog(AccessControl.this.getShell(), AbstractUserObject.class);
142 if (dlg.open() == Window.OK)
143 {
144 AbstractUserObject[] selection = dlg.getSelection();
145 for(AbstractUserObject user : selection)
146 acl.put(user.getId(), new AccessListElement(user.getId(), 0));
147 userList.setInput(acl.values().toArray());
148 }
149 }
150 });
151
152 final Button deleteButton = new Button(buttons, SWT.PUSH);
153 deleteButton.setText(Messages.get().AccessControl_Delete);
154 deleteButton.setEnabled(false);
155 deleteButton.addSelectionListener(new SelectionListener() {
156 @Override
157 public void widgetDefaultSelected(SelectionEvent e)
158 {
159 widgetSelected(e);
160 }
161
162 @SuppressWarnings("unchecked")
163 @Override
164 public void widgetSelected(SelectionEvent e)
165 {
166 IStructuredSelection sel = (IStructuredSelection)userList.getSelection();
167 Iterator<AccessListElement> it = sel.iterator();
168 while(it.hasNext())
169 {
170 AccessListElement element = it.next();
171 acl.remove(element.getUserId());
172 }
173 userList.setInput(acl.values().toArray());
174 }
175 });
176
177 Group rights = new Group(dialogArea, SWT.NONE);
178 rights.setText(Messages.get().AccessControl_AccessRights);
179 rights.setLayout(new RowLayout(SWT.VERTICAL));
180 gd = new GridData();
181 gd.grabExcessVerticalSpace = true;
182 gd.horizontalAlignment = SWT.FILL;
183 gd.verticalAlignment = SWT.FILL;
184 rights.setLayoutData(gd);
185
186 createAccessCheck(rights, Messages.get().AccessControl_AccessRead, UserAccessRights.OBJECT_ACCESS_READ);
187 createAccessCheck(rights, Messages.get().AccessControl_AccessModify, UserAccessRights.OBJECT_ACCESS_MODIFY);
188 createAccessCheck(rights, Messages.get().AccessControl_AccessCreate, UserAccessRights.OBJECT_ACCESS_CREATE);
189 createAccessCheck(rights, Messages.get().AccessControl_AccessDelete, UserAccessRights.OBJECT_ACCESS_DELETE);
190 createAccessCheck(rights, Messages.get().AccessControl_AccessControl, UserAccessRights.OBJECT_ACCESS_CONTROL);
191 createAccessCheck(rights, Messages.get().AccessControl_AccessSendEvents, UserAccessRights.OBJECT_ACCESS_SEND_EVENTS);
192 createAccessCheck(rights, Messages.get().AccessControl_AccessViewAlarms, UserAccessRights.OBJECT_ACCESS_READ_ALARMS);
193 createAccessCheck(rights, Messages.get().AccessControl_AccessUpdateAlarms, UserAccessRights.OBJECT_ACCESS_UPDATE_ALARMS);
194 createAccessCheck(rights, Messages.get().AccessControl_AccessTermAlarms, UserAccessRights.OBJECT_ACCESS_TERM_ALARMS);
195 createAccessCheck(rights, Messages.get().AccessControl_CreateTicket, UserAccessRights.OBJECT_ACCESS_CREATE_ISSUE);
196 createAccessCheck(rights, Messages.get().AccessControl_AccessPushData, UserAccessRights.OBJECT_ACCESS_PUSH_DATA);
197 createAccessCheck(rights, Messages.get().AccessControl_AccessAccessControl, UserAccessRights.OBJECT_ACCESS_ACL);
198 createAccessCheck(rights, Messages.get().AccessControl_DownloadFiles, UserAccessRights.OBJECT_ACCESS_DOWNLOAD);
199 createAccessCheck(rights, Messages.get().AccessControl_UploadFiles, UserAccessRights.OBJECT_ACCESS_UPLOAD);
200 createAccessCheck(rights, Messages.get().AccessControl_ManageFiles, UserAccessRights.OBJECT_ACCESS_MANAGE_FILES);
201 createAccessCheck(rights, "Control maintenance mode", UserAccessRights.OBJECT_ACCESS_MAINTENANCE);
202
203 userList.addSelectionChangedListener(new ISelectionChangedListener() {
204 @Override
205 public void selectionChanged(SelectionChangedEvent event)
206 {
207 IStructuredSelection sel = (IStructuredSelection)event.getSelection();
208 if (sel.size() == 1)
209 {
210 enableAllChecks(true);
211 AccessListElement element = (AccessListElement)sel.getFirstElement();
212 int rights = element.getAccessRights();
213 for(int i = 0, mask = 1; i < 16; i++, mask <<= 1)
214 {
215 Button check = accessChecks.get(mask);
216 if (check != null)
217 {
218 check.setSelection((rights & mask) == mask);
219 }
220 }
221 }
222 else
223 {
224 enableAllChecks(false);
225 }
226 deleteButton.setEnabled(sel.size() > 0);
227 }
228 });
229
230 checkInherit = new Button(dialogArea, SWT.CHECK);
231 checkInherit.setText(Messages.get().AccessControl_InheritRights);
232 if (object.getParentCount() > 0)
233 {
234 checkInherit.setSelection(object.isInheritAccessRights());
235 }
236 else
237 {
238 // For objects without parent "inherit access rights" option is meaningless
239 checkInherit.setSelection(false);
240 checkInherit.setEnabled(false);
241 }
242
243 return dialogArea;
244 }
245
246 /**
247 * Create access control check box.
248 *
249 * @param parent Parent composite
250 * @param name Name of the access right
251 * @param bitMask Bit mask for access right
252 */
253 private void createAccessCheck(final Composite parent, final String name, final Integer bitMask)
254 {
255 final Button check = new Button(parent, SWT.CHECK);
256 check.setText(name);
257 check.setEnabled(false);
258 check.addSelectionListener(new SelectionListener() {
259 @Override
260 public void widgetDefaultSelected(SelectionEvent e)
261 {
262 widgetSelected(e);
263 }
264
265 @Override
266 public void widgetSelected(SelectionEvent e)
267 {
268 IStructuredSelection sel = (IStructuredSelection)userList.getSelection();
269 AccessListElement element = (AccessListElement)sel.getFirstElement();
270 int rights = element.getAccessRights();
271 if (check.getSelection())
272 rights |= bitMask;
273 else
274 rights &= ~bitMask;
275 element.setAccessRights(rights);
276 userList.update(element, null);
277 }
278 });
279 accessChecks.put(bitMask, check);
280 }
281
282 /**
283 * Enables all access check boxes if the argument is true, and disables them otherwise.
284 *
285 * @param enabled the new enabled state
286 */
287 private void enableAllChecks(boolean enabled)
288 {
289 for(final Button b : accessChecks.values())
290 {
291 b.setEnabled(enabled);
292 }
293 }
294
295 /**
296 * Apply changes
297 *
298 * @param isApply true if update operation caused by "Apply" button
299 */
300 protected void applyChanges(final boolean isApply)
301 {
302 if (isApply)
303 setValid(false);
304
305 final boolean inheritAccessRights = checkInherit.getSelection();
306 final NXCSession session = (NXCSession)ConsoleSharedData.getSession();
307 final NXCObjectModificationData md = new NXCObjectModificationData(object.getObjectId());
308 md.setACL(acl.values().toArray(new AccessListElement[acl.size()]));
309 md.setInheritAccessRights(inheritAccessRights);
310
311 new ConsoleJob(String.format(Messages.get().AccessControl_JobName, object.getObjectName()), null, Activator.PLUGIN_ID, null) {
312 @Override
313 protected void runInternal(IProgressMonitor monitor) throws Exception
314 {
315 session.modifyObject(md);
316 }
317
318 @Override
319 protected void jobFinalize()
320 {
321 if (isApply)
322 {
323 runInUIThread(new Runnable() {
324 @Override
325 public void run()
326 {
327 AccessControl.this.setValid(true);
328 }
329 });
330 }
331 }
332
333 @Override
334 protected String getErrorMessage()
335 {
336 return Messages.get().AccessControl_JobError;
337 }
338 }.start();
339 }
340
341 /* (non-Javadoc)
342 * @see org.eclipse.jface.preference.PreferencePage#performOk()
343 */
344 @Override
345 public boolean performOk()
346 {
347 applyChanges(false);
348 return true;
349 }
350
351 /* (non-Javadoc)
352 * @see org.eclipse.jface.preference.PreferencePage#performApply()
353 */
354 @Override
355 protected void performApply()
356 {
357 applyChanges(true);
358 }
359
360 /* (non-Javadoc)
361 * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
362 */
363 @Override
364 protected void performDefaults()
365 {
366 super.performDefaults();
367 checkInherit.setSelection(true);
368 acl.clear();
369 userList.setInput(acl.values().toArray());
370 }
371 }