View Javadoc
1   /*
2    * Copyright (C) 2003-2007 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.ecm.webui.form.validator;
18  
19  import org.exoplatform.services.organization.OrganizationService;
20  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
21  import org.exoplatform.web.application.ApplicationMessage;
22  import org.exoplatform.webui.exception.MessageException;
23  import org.exoplatform.webui.form.UIFormInput;
24  import org.exoplatform.webui.form.validator.Validator;
25  
26  /**
27   * Created by The eXo Platform SEA
28   * Author : Ha Quang Tan
29   *          tanhq@exoplatform.com
30   * May 16, 2011 4:52:44 PM
31   */
32  
33  public class PermissionValidator implements Validator {
34    public void validate(UIFormInput uiInput) throws Exception {
35      if (uiInput.getValue() == null || ((String) uiInput.getValue()).trim().length() == 0
36          || ((String) uiInput.getValue()).trim().equals("*"))
37        return;
38      String permissions = ((String) uiInput.getValue()).trim();
39  
40      OrganizationService oservice = WCMCoreUtils.getService(OrganizationService.class);
41      String[] arrPermissions = permissions.split(",");
42      for (String itemPermission : arrPermissions) {
43        if (itemPermission.length() == 0) {
44          Object[] args = { itemPermission };
45          throw new MessageException(new ApplicationMessage("PermissionValidator.msg.permission-path-invalid",
46                                                            args));
47        }
48        if (itemPermission.equals("*"))
49          continue;
50        else if (itemPermission.contains(":")) {
51          String[] permission = itemPermission.split(":");
52          if ((permission[0] == null) || (permission[0].length() == 0)) {
53            Object[] args = { itemPermission };
54            throw new MessageException(new ApplicationMessage("PermissionValidator.msg.permission-path-invalid",
55                                                              args));
56          } else if (!permission[0].equals("*") && (oservice.getMembershipTypeHandler().findMembershipType(permission[0]) == null)) {
57            Object[] args = { itemPermission };
58            throw new MessageException(new ApplicationMessage("PermissionValidator.msg.permission-path-invalid",
59                                                              args));
60          }
61          if ((permission[1] == null) || (permission[1].length() == 0)) {
62            Object[] args = { itemPermission };
63            throw new MessageException(new ApplicationMessage("PermissionValidator.msg.permission-path-invalid",
64                                                              args));
65          } else if (oservice.getGroupHandler().findGroupById(permission[1]) == null) {
66            Object[] args = { itemPermission };
67            throw new MessageException(new ApplicationMessage("PermissionValidator.msg.permission-path-invalid",
68                                                              args));
69          }
70        } else {
71          Object[] args = { itemPermission };
72          throw new MessageException(new ApplicationMessage("PermissionValidator.msg.permission-path-invalid",
73                                                            args));
74        }
75      }
76    }
77  }