2 * Copyright 2011 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and
31 * documentation are those of the authors and should not be
32 * interpreted as representing official policies, either expressed
33 * or implied, of GRNET S.A.
35 package gr.grnet.pithos.web.client;
37 import gr.grnet.pithos.web.client.FilePropertiesDialog.Images;
38 import gr.grnet.pithos.web.client.rest.GetCommand;
39 import gr.grnet.pithos.web.client.rest.resource.PermissionHolder;
40 import gr.grnet.pithos.web.client.rest.resource.UserResource;
41 import gr.grnet.pithos.web.client.rest.resource.UserSearchResource;
43 import java.util.HashSet;
46 import com.google.gwt.event.dom.client.ClickEvent;
47 import com.google.gwt.event.dom.client.ClickHandler;
48 import com.google.gwt.user.client.DeferredCommand;
49 import com.google.gwt.user.client.ui.AbstractImagePrototype;
50 import com.google.gwt.user.client.ui.CheckBox;
51 import com.google.gwt.user.client.ui.Composite;
52 import com.google.gwt.user.client.ui.FlexTable;
53 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
54 import com.google.gwt.user.client.ui.PushButton;
55 import com.google.gwt.user.client.ui.VerticalPanel;
58 public class PermissionsList extends Composite {
62 int permissionCount = -1;
64 Set<PermissionHolder> permissions = null;
68 final VerticalPanel permPanel = new VerticalPanel();
70 final FlexTable permTable = new FlexTable();
74 PermissionHolder toRemove = null;
76 private boolean hasChanges = false;
78 private boolean hasAddition = false;
82 public PermissionsList(Pithos _app, final Images theImages, Set<PermissionHolder> thePermissions, String anOwner){
86 permissions = new HashSet<PermissionHolder>();
87 permissions.addAll(thePermissions);
88 permTable.setText(0, 0, "Users/Groups");
89 permTable.setText(0, 1, "Read");
90 permTable.setText(0, 2, "Write");
91 permTable.setText(0, 3, "Modify Access");
92 permTable.setText(0, 4, "");
93 permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
94 permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
95 permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
96 permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
97 permPanel.add(permTable);
98 permPanel.addStyleName("pithos-TabPanelBottom");
99 initWidget(permPanel);
103 public boolean hasChanges(){
104 return hasChanges || hasAddition;
108 public void updateTable(){
109 copySetAndContinue(permissions);
112 public void updatePermissionsAccordingToInput(){
114 for(PermissionHolder dto : permissions){
115 /*if(dto.getId() == null)
117 CheckBox r = (CheckBox) permTable.getWidget(i, 1);
118 CheckBox w = (CheckBox) permTable.getWidget(i, 2);
119 CheckBox m = (CheckBox) permTable.getWidget(i, 3);
121 r.getElement().setId("permissionList.read");
122 w.getElement().setId("permissionList.write");
123 m.getElement().setId("permissionList.modify");
125 if(dto.isRead() != r.getValue() || dto.isWrite() != w.getValue() || dto.isModifyACL() != m.getValue())
127 dto.setRead(r.getValue());
128 dto.setWrite(w.getValue());
129 dto.setModifyACL(m.getValue());
135 * Retrieve the permissions.
137 * @return the permissions
139 public Set<PermissionHolder> getPermissions() {
143 public void addPermission(PermissionHolder permission){
144 permissions.add(permission);
148 * Copies the input Set to a new Set
151 private void copySetAndContinue(Set<PermissionHolder> input){
152 Set<PermissionHolder> copiedInput = new HashSet<PermissionHolder>();
153 for(PermissionHolder dto : input) {
154 copiedInput.add(dto);
156 handleFullNames(copiedInput);
160 * Examines whether or not the user's full name exists in the
161 * userFullNameMap in the Pithos.java for every element of the input list.
162 * If the user's full name does not exist in the map then a request is being made
163 * for the specific username.
166 private void handleFullNames(Set<PermissionHolder> aPermissions){
167 if(aPermissions.isEmpty()){
168 showPermissionTable();
172 final PermissionHolder dto = aPermissions.iterator().next();
173 if(dto.getGroup() != null){
174 if(aPermissions.size() >= 1){
175 aPermissions.remove(dto);
176 handleFullNames(aPermissions);
178 }else if(app.findUserFullName(dto.getUser()) != null){
179 if(aPermissions.size() >= 1){
180 aPermissions.remove(dto);
181 handleFullNames(aPermissions);
184 findFullNameAndUpdate(aPermissions);
189 * Shows the permission table
192 private void showPermissionTable(){
194 if(toRemove != null){
195 permissions.remove(toRemove);
198 for(final PermissionHolder dto : permissions){
199 PushButton removeButton = new PushButton(AbstractImagePrototype.create(images.delete()).createImage(), new ClickHandler() {
201 public void onClick(ClickEvent event) {
208 if(dto.getUser() != null){
209 if(dto.getUser() != null && dto.getUser().equals(owner)){
210 permTable.setHTML(i, 0, "<span id=permissionList.Owner>" + AbstractImagePrototype.create(images.permUser()).getHTML() + " Owner</span>");
211 removeButton.setVisible(false);
213 permTable.setHTML(i, 0, "<span id=permissionList."+ app.findUserFullName(dto.getUser())+">"+ AbstractImagePrototype.create(images.permUser()).getHTML() + " "+ app.findUserFullName(dto.getUser()) + "</span>");
215 }else if(dto.getGroup() != null){
216 permTable.setHTML(i, 0, "<span id=permissionList."+dto.getGroup()+">" + AbstractImagePrototype.create(images.permGroup()).getHTML() + " "+ dto.getGroup() + "</span>");
219 CheckBox read = new CheckBox();
220 read.setValue(dto.isRead());
221 read.getElement().setId("permissionList.read");
223 CheckBox write = new CheckBox();
224 write.setValue(dto.isWrite());
225 write.getElement().setId("permissionList.write");
227 CheckBox modify = new CheckBox();
228 modify.setValue(dto.isModifyACL());
229 modify.getElement().setId("permissionList.modify");
231 if (dto.getUser()!=null && dto.getUser().equals(owner)) {
232 read.setEnabled(false);
233 write.setEnabled(false);
234 modify.setEnabled(false);
237 permTable.setWidget(i, 1, read);
238 permTable.setWidget(i, 2, write);
239 permTable.setWidget(i, 3, modify);
240 permTable.setWidget(i, 4, removeButton);
241 permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
242 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
243 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
244 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
247 for(; i<permTable.getRowCount(); i++)
248 permTable.removeRow(i);
253 * Makes a request to search for full name from a given username
254 * and continues checking the next element of the Set.
258 private void findFullNameAndUpdate(final Set<PermissionHolder> aPermissions){
259 final PermissionHolder dto = aPermissions.iterator().next();
260 String path = app.getApiPath() + "users/" + dto.getUser();
262 GetCommand<UserSearchResource> gg = new GetCommand<UserSearchResource>(app, UserSearchResource.class, path, false,null) {
264 public void onComplete() {
265 final UserSearchResource result = getResult();
266 for (UserResource user : result.getUsers()){
267 String username = user.getUsername();
268 String userFullName = user.getName();
269 app.putUserToMap(username, userFullName);
270 if(aPermissions.size() >= 1){
271 aPermissions.remove(dto);
272 if(aPermissions.isEmpty()){
273 showPermissionTable();
276 handleFullNames(aPermissions);
281 public void onError(Throwable t) {
282 app.displayError("Unable to fetch user's full name from the given username " + dto.getUser());
283 if(aPermissions.size() >= 1){
284 aPermissions.remove(dto);
285 if(aPermissions.isEmpty()){
286 showPermissionTable();
289 handleFullNames(aPermissions);
293 DeferredCommand.addCommand(gg);