Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / PermissionsAddDialog.java @ d1609e26

History | View | Annotate | Download (8.2 kB)

1
/*
2
 * Copyright 2011-2013 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
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.
16
 *
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.
29
 *
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.
34
 */
35
package gr.grnet.pithos.web.client;
36

    
37
import com.google.gwt.dom.client.NativeEvent;
38
import com.google.gwt.event.dom.client.ClickEvent;
39
import com.google.gwt.event.dom.client.ClickHandler;
40
import com.google.gwt.event.dom.client.KeyCodes;
41
import com.google.gwt.regexp.shared.RegExp;
42
import com.google.gwt.user.client.Event.NativePreviewEvent;
43
import com.google.gwt.user.client.ui.*;
44
import gr.grnet.pithos.web.client.catalog.UpdateUserCatalogs;
45
import gr.grnet.pithos.web.client.catalog.UserCatalogs;
46
import gr.grnet.pithos.web.client.grouptree.Group;
47

    
48
import java.util.List;
49

    
50
public class PermissionsAddDialog extends DialogBox {
51
    final static RegExp EmailValidator = RegExp.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+[.][A-Z]{2,4}$", "i");
52

    
53
    private TextBox userBox = new TextBox();
54

    
55
    private ListBox groupBox = new ListBox();
56

    
57
    private RadioButton read = new RadioButton("permissions");
58

    
59
    private RadioButton write = new RadioButton("permissions");
60

    
61
    private final PermissionsList permList;
62

    
63
    private final boolean isUser;
64

    
65
    private final Pithos app;
66

    
67
    public PermissionsAddDialog(Pithos app, List<Group> groups, PermissionsList permList, boolean isUser) {
68
        this.app = app;
69
        this.isUser = isUser;
70
        this.permList = permList;
71

    
72
        Anchor close = new Anchor("close");
73
        close.addStyleName("close");
74
        close.addClickHandler(new ClickHandler() {
75
            @Override
76
            public void onClick(ClickEvent event) {
77
                hide();
78
            }
79
        });
80
        final String dialogText = isUser ? Const.TXT_ADD_USER : Const.TXT_ADD_GROUP;
81
        setText(dialogText);
82
        setStyleName("pithos-DialogBox");
83

    
84
        final VerticalPanel panel = new VerticalPanel();
85
        panel.add(close);
86

    
87
        VerticalPanel inner = new VerticalPanel();
88
        inner.addStyleName("inner");
89

    
90
        final FlexTable permTable = new FlexTable();
91
        permTable.setText(0, 0, isUser ? Const.TXT_USER : Const.TXT_USER);
92
        permTable.setText(0, 1, "Read Only");
93
        permTable.setText(0, 2, "Read/Write");
94
        permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
95
        permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
96
        permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
97

    
98
        if(this.isUser) {
99
            permTable.setWidget(1, 0, userBox);
100
        }
101
        else {
102
            for(Group group : groups) {
103
                groupBox.addItem(group.getName(), group.getName());
104
            }
105
            permTable.setWidget(1, 0, groupBox);
106
        }
107

    
108
        read.setValue(true);
109
        permTable.setWidget(1, 1, read);
110
        permTable.setWidget(1, 2, write);
111

    
112
        permTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
113
        permTable.getFlexCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_CENTER);
114
        permTable.getFlexCellFormatter().setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_CENTER);
115
        inner.add(permTable);
116

    
117
        final Button ok = new Button("OK", new ClickHandler() {
118
            @Override
119
            public void onClick(ClickEvent event) {
120
                addPermission();
121
                hide();
122
            }
123
        });
124

    
125
        ok.addStyleName("button");
126
        inner.add(ok);
127

    
128
        panel.add(inner);
129
        panel.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
130

    
131
        setWidget(panel);
132
    }
133

    
134
    protected void addPermission() {
135
        final boolean readValue = read.getValue();
136
        final boolean writeValue = write.getValue();
137

    
138
        String selected = null;
139
        if(isUser) {
140
            final String userDisplayName = userBox.getText().trim();
141
            addUserPermission(userDisplayName, readValue, writeValue);
142
            return;
143
        }
144
        else if(groupBox.getSelectedIndex() > -1) {
145
            String groupName = groupBox.getValue(groupBox.getSelectedIndex());
146
            selected = app.getUserID() + ":" + groupName;
147
        }
148
        if(permList.getPermissions().get(selected) != null) {
149
            return;
150
        }
151
        if(selected == null || selected.length() == 0 || selected.equals(app.getUserID() + ":")) {
152
            app.displayWarning("You have to select a username or group");
153
            return;
154
        }
155

    
156
        permList.addPermission(selected, readValue, writeValue);
157
    }
158

    
159
    private boolean alreadyHasPermission(String selected) {
160
        return permList.getPermissions().get(selected) != null;
161
    }
162

    
163
    private void addUserPermission(final String userDisplayName, final boolean readValue, final boolean writeValue) {
164
        if(!EmailValidator.test(userDisplayName)) {
165
            app.displayWarning("Username must be a valid email address");
166
            return;
167
        }
168

    
169
        // Now get the userID
170
        final String userID = app.getIDForUserDisplayName(userDisplayName);
171
        if(userID != null) {
172
            // Check if already have the permission
173
            if(!alreadyHasPermission(userID)) {
174
                permList.addPermission(userID, readValue, writeValue);
175
            }
176
        }
177
        else {
178
            // Must call server to obtain userID
179
            new UpdateUserCatalogs(app, null, Helpers.toList(userDisplayName)) {
180
                @Override
181
                public void onSuccess(UserCatalogs requestedUserCatalogs, UserCatalogs updatedUserCatalogs) {
182
                    final String userID = updatedUserCatalogs.getID(userDisplayName);
183
                    if(userID == null) {
184
                        Pithos.LOG("PermissionsDialog::addUserPermission(), UpdateUserCatalogs() => Unknown user ", userDisplayName);
185
                        app.displayWarning("Unknown user " + userDisplayName);
186
                    }
187
                    else if(!alreadyHasPermission(userID)) {
188
                        permList.addPermission(userID, readValue, writeValue);
189
                    }
190
                }
191
            }.scheduleDeferred();
192
        }
193
    }
194

    
195
    @Override
196
    protected void onPreviewNativeEvent(NativePreviewEvent preview) {
197
        super.onPreviewNativeEvent(preview);
198

    
199
        NativeEvent evt = preview.getNativeEvent();
200
        if(evt.getType().equals("keydown"))
201
        // Use the popup's key preview hooks to close the dialog when either
202
        // enter or escape is pressed.
203
        {
204
            switch(evt.getKeyCode()) {
205
                case KeyCodes.KEY_ENTER:
206
                    addPermission();
207
                    hide();
208
                    break;
209
                case KeyCodes.KEY_ESCAPE:
210
                    hide();
211
                    break;
212
            }
213
        }
214
    }
215

    
216

    
217
    @Override
218
    public void center() {
219
        super.center();
220
        if(isUser) {
221
            userBox.setFocus(true);
222
        }
223
    }
224
}