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.security.auth;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.security.permission.PermissionChecker;
021    
022    /**
023     * @author Brian Wing Shun Chan
024     */
025    public class PrincipalException extends PortalException {
026    
027            public static Class<?>[] getNestedClasses() {
028                    return _NESTED_CLASSES;
029            }
030    
031            public PrincipalException() {
032            }
033    
034            public PrincipalException(String msg) {
035                    super(msg);
036            }
037    
038            public PrincipalException(String msg, Throwable cause) {
039                    super(msg, cause);
040            }
041    
042            public PrincipalException(Throwable cause) {
043                    super(cause);
044            }
045    
046            public static class MustBeAuthenticated extends PrincipalException {
047    
048                    public MustBeAuthenticated(long userId) {
049                            this(String.valueOf(userId));
050                    }
051    
052                    public MustBeAuthenticated(String login) {
053                            super(String.format("User %s must be authenticated", login));
054    
055                            this.login = login;
056                    }
057    
058                    public final String login;
059    
060            }
061    
062            public static class MustBeCompanyAdmin extends PrincipalException {
063    
064                    public MustBeCompanyAdmin(long userId) {
065                            super(
066                                    String.format(
067                                            "User %s must be the company administrator to perform " +
068                                                    "the action",
069                                            userId));
070    
071                            this.userId = userId;
072                    }
073    
074                    public MustBeCompanyAdmin(PermissionChecker permissionChecker) {
075                            this(permissionChecker.getUserId());
076                    }
077    
078                    public final long userId;
079    
080            }
081    
082            public static class MustBeEnabled extends PrincipalException {
083    
084                    public MustBeEnabled(long companyId, String ... resourceName) {
085                            super(
086                                    String.format(
087                                            "%s must be enabled for company %s",
088                                            StringUtil.merge(resourceName, ","), companyId));
089    
090                            this.companyId = companyId;
091                            this.resourceName = resourceName;
092                    }
093    
094                    public final long companyId;
095                    public final String[] resourceName;
096    
097            }
098    
099            public static class MustBeInvokedUsingPost extends PrincipalException {
100    
101                    public MustBeInvokedUsingPost(String url) {
102                            super(String.format("URL %s must be invoked using POST", url));
103    
104                            this.url = url;
105                    }
106    
107                    public final String url;
108    
109            }
110    
111            public static class MustBeOmniadmin extends PrincipalException {
112    
113                    public MustBeOmniadmin(long userId) {
114                            super(
115                                    String.format(
116                                            "User %s must be a universal administrator to perform " +
117                                                    "the action",
118                                            userId));
119    
120                            this.userId = userId;
121                    }
122    
123                    public MustBeOmniadmin(PermissionChecker permissionChecker) {
124                            this(permissionChecker.getUserId());
125                    }
126    
127                    public final long userId;
128    
129            }
130    
131            public static class MustBePortletStrutsPath extends PrincipalException {
132    
133                    public MustBePortletStrutsPath(String strutsPath, String portletId) {
134                            super(
135                                    String.format(
136                                            "Struts path %s must be struts path of portlet %s",
137                                            strutsPath, portletId));
138    
139                            this.strutsPath = strutsPath;
140                            this.portletId = portletId;
141                    }
142    
143                    public final String portletId;
144                    public final String strutsPath;
145    
146            }
147    
148            public static class MustHavePermission extends PrincipalException {
149    
150                    public MustHavePermission(long userId, String... actionIds) {
151                            super(
152                                    String.format(
153                                            "User %s must have permission to perform action %s",
154                                            GetterUtil.getString(userId),
155                                            StringUtil.merge(actionIds, ",")));
156    
157                            this.actionId = actionIds;
158                            this.resourceId = 0;
159                            this.resourceName = null;
160                            this.userId = userId;
161                    }
162    
163                    public MustHavePermission(
164                            long userId, String resourceName, long resourceId,
165                            String... actionIds) {
166    
167                            super(
168                                    String.format(
169                                            "User %s must have %s permission for %s %s",
170                                            GetterUtil.getString(userId),
171                                            StringUtil.merge(actionIds, ","), resourceName,
172                                            GetterUtil.getString(resourceId)));
173    
174                            this.actionId = actionIds;
175                            this.resourceName = resourceName;
176                            this.resourceId = resourceId;
177                            this.userId = userId;
178                    }
179    
180                    public MustHavePermission(
181                            PermissionChecker permissionChecker, String... actionIds) {
182    
183                            this(permissionChecker.getUserId(), actionIds);
184                    }
185    
186                    public MustHavePermission(
187                            PermissionChecker permissionChecker, String resourceName,
188                            long resourceId, String... actionIds) {
189    
190                            this(
191                                    permissionChecker.getUserId(), resourceName, resourceId,
192                                    actionIds);
193                    }
194    
195                    public final String[] actionId;
196                    public final long resourceId;
197                    public final String resourceName;
198                    public final long userId;
199    
200            }
201    
202            private static final Class<?>[] _NESTED_CLASSES = {
203                    PrincipalException.class, PrincipalException.MustBeAuthenticated.class,
204                    PrincipalException.MustBeCompanyAdmin.class,
205                    PrincipalException.MustBeEnabled.class,
206                    PrincipalException.MustBeInvokedUsingPost.class,
207                    PrincipalException.MustBeOmniadmin.class,
208                    PrincipalException.MustBePortletStrutsPath.class,
209                    PrincipalException.MustHavePermission.class
210            };
211    
212    }