001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.security.auth;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.security.permission.PermissionChecker;
019    import com.liferay.portal.kernel.util.StringUtil;
020    
021    /**
022     * @author Brian Wing Shun Chan
023     */
024    public class PrincipalException extends PortalException {
025    
026            public static Class<?>[] getNestedClasses() {
027                    return _NESTED_CLASSES;
028            }
029    
030            public PrincipalException() {
031            }
032    
033            public PrincipalException(String msg) {
034                    super(msg);
035            }
036    
037            public PrincipalException(String msg, Throwable cause) {
038                    super(msg, cause);
039            }
040    
041            public PrincipalException(Throwable cause) {
042                    super(cause);
043            }
044    
045            public static class MustBeAuthenticated extends PrincipalException {
046    
047                    public MustBeAuthenticated(long userId) {
048                            this(String.valueOf(userId));
049                    }
050    
051                    public MustBeAuthenticated(String login) {
052                            super(String.format("User %s must be authenticated", login));
053    
054                            this.login = login;
055                    }
056    
057                    public final String login;
058    
059            }
060    
061            public static class MustBeCompanyAdmin extends PrincipalException {
062    
063                    public MustBeCompanyAdmin(long userId) {
064                            super(
065                                    String.format(
066                                            "User %s must be the company administrator to perform " +
067                                                    "the action",
068                                            userId));
069    
070                            this.userId = userId;
071                    }
072    
073                    public MustBeCompanyAdmin(PermissionChecker permissionChecker) {
074                            this(permissionChecker.getUserId());
075                    }
076    
077                    public final long userId;
078    
079            }
080    
081            public static class MustBeEnabled extends PrincipalException {
082    
083                    public MustBeEnabled(long companyId, String... resourceName) {
084                            super(
085                                    String.format(
086                                            "%s must be enabled for company %s",
087                                            StringUtil.merge(resourceName, ","), companyId));
088    
089                            this.companyId = companyId;
090                            this.resourceName = resourceName;
091                    }
092    
093                    public final long companyId;
094                    public final String[] resourceName;
095    
096            }
097    
098            public static class MustBeInvokedUsingPost extends PrincipalException {
099    
100                    public MustBeInvokedUsingPost(String url) {
101                            super(String.format("URL %s must be invoked using POST", url));
102    
103                            this.url = url;
104                    }
105    
106                    public final String url;
107    
108            }
109    
110            public static class MustBeOmniadmin extends PrincipalException {
111    
112                    public MustBeOmniadmin(long userId) {
113                            super(
114                                    String.format(
115                                            "User %s must be a universal administrator to perform " +
116                                                    "the action",
117                                            userId));
118    
119                            this.userId = userId;
120                    }
121    
122                    public MustBeOmniadmin(PermissionChecker permissionChecker) {
123                            this(permissionChecker.getUserId());
124                    }
125    
126                    public final long userId;
127    
128            }
129    
130            public static class MustBePortletStrutsPath extends PrincipalException {
131    
132                    public MustBePortletStrutsPath(String strutsPath, String portletId) {
133                            super(
134                                    String.format(
135                                            "Struts path %s must be struts path of portlet %s",
136                                            strutsPath, portletId));
137    
138                            this.strutsPath = strutsPath;
139                            this.portletId = portletId;
140                    }
141    
142                    public final String portletId;
143                    public final String strutsPath;
144    
145            }
146    
147            public static class MustHavePermission extends PrincipalException {
148    
149                    public MustHavePermission(long userId, String... actionIds) {
150                            super(
151                                    String.format(
152                                            "User %s must have permission to perform action %s", userId,
153                                            StringUtil.merge(actionIds, ",")));
154    
155                            this.actionId = actionIds;
156                            this.resourceId = 0;
157                            this.resourceName = null;
158                            this.userId = userId;
159                    }
160    
161                    public MustHavePermission(
162                            long userId, String resourceName, long resourceId,
163                            String... actionIds) {
164    
165                            super(
166                                    String.format(
167                                            "User %s must have %s permission for %s %s", userId,
168                                            StringUtil.merge(actionIds, ","), resourceName,
169                                            resourceId));
170    
171                            this.actionId = actionIds;
172                            this.resourceName = resourceName;
173                            this.resourceId = resourceId;
174                            this.userId = userId;
175                    }
176    
177                    public MustHavePermission(
178                            PermissionChecker permissionChecker, String... actionIds) {
179    
180                            this(permissionChecker.getUserId(), actionIds);
181                    }
182    
183                    public MustHavePermission(
184                            PermissionChecker permissionChecker, String resourceName,
185                            long resourceId, String... actionIds) {
186    
187                            this(
188                                    permissionChecker.getUserId(), resourceName, resourceId,
189                                    actionIds);
190                    }
191    
192                    public final String[] actionId;
193                    public final long resourceId;
194                    public final String resourceName;
195                    public final long userId;
196    
197            }
198    
199            private static final Class<?>[] _NESTED_CLASSES = {
200                    PrincipalException.class, PrincipalException.MustBeAuthenticated.class,
201                    PrincipalException.MustBeCompanyAdmin.class,
202                    PrincipalException.MustBeEnabled.class,
203                    PrincipalException.MustBeInvokedUsingPost.class,
204                    PrincipalException.MustBeOmniadmin.class,
205                    PrincipalException.MustBePortletStrutsPath.class,
206                    PrincipalException.MustHavePermission.class
207            };
208    
209    }