View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.user2;
20  
21  import java.net.URLEncoder;
22  import java.nio.charset.StandardCharsets;
23  import java.util.HashMap;
24  import java.util.LinkedHashMap;
25  import java.util.Map.Entry;
26  
27  import org.mycore.common.MCRConstants;
28  import org.mycore.common.MCRSessionMgr;
29  import org.mycore.common.config.MCRConfiguration2;
30  
31  /**
32   * Represents a realm of users. Each user belongs to a realm. Realms are configured 
33   * in the file realms.xml. A realm determines the method that is used to login the user.
34   * There is always a local default realm, which is defined by the attribute local in realms.xml.
35   * 
36   * @author Frank L\u00fctzenkirchen
37   * @author Thomas Scheffler (yagee)
38   */
39  public class MCRRealm {
40      /** The unique ID of the realm, e.g. 'local' */
41      private String id;
42  
43      /** The labels of the realm */
44      private HashMap<String, String> labels = new HashMap<>();
45  
46      /** The URL where users from this realm can change their password */
47      private String passwordChangeURL;
48  
49      /** The URL where users from this realm can login */
50      private String loginURL;
51  
52      /** The URL where new users may create an account for this realm  */
53      private String createURL;
54  
55      private String redirectParameter;
56  
57      private String realmParameter;
58  
59      private static String DEFAULT_LANG = MCRConfiguration2.getString("MCR.Metadata.DefaultLang")
60          .orElse(MCRConstants.DEFAULT_LANG);
61  
62      public static final String USER_INFORMATION_ATTR = "realmId";
63  
64      /** 
65       * Creates a new realm.
66       * 
67       * @param id the unique ID of the realm
68       */
69      MCRRealm(String id) {
70          this.id = id;
71      }
72  
73      /**
74       * Returns the unique ID of the realm.
75       * 
76       * @return the unique ID of the realm.
77       */
78      public String getID() {
79          return id;
80      }
81  
82      /**
83       * Returns the label in the current language
84       */
85      public String getLabel() {
86          String lang = MCRSessionMgr.getCurrentSession().getCurrentLanguage();
87          String label = labels.get(lang);
88          if (label != null) {
89              return label;
90          }
91          label = labels.get(DEFAULT_LANG);
92          if (label != null) {
93              return label;
94          }
95          return id;
96      }
97  
98      /**
99       * Sets the label for the given language
100      */
101     void setLabel(String lang, String label) {
102         labels.put(lang, label);
103     }
104 
105     /** 
106      * Returns the URL where users from this realm can change their password 
107      */
108     public String getPasswordChangeURL() {
109         return passwordChangeURL;
110     }
111 
112     /** 
113      * Sets the URL where users from this realm can change their password 
114      */
115     void setPasswordChangeURL(String url) {
116         this.passwordChangeURL = url;
117     }
118 
119     /** 
120      * Returns the URL where users from this realm can login 
121      */
122     public String getLoginURL() {
123         return loginURL;
124     }
125 
126     /** 
127      * Sets the URL where users from this realm can login 
128      */
129     void setLoginURL(String url) {
130         this.loginURL = url;
131     }
132 
133     /**
134      * @return the createURL
135      */
136     public String getCreateURL() {
137         return createURL;
138     }
139 
140     /**
141      * @param createURL the createURL to set
142      */
143     void setCreateURL(String createURL) {
144         this.createURL = createURL;
145     }
146 
147     @Override
148     public boolean equals(Object obj) {
149         if (obj instanceof MCRRealm) {
150             return ((MCRRealm) obj).id.equals(id);
151         } else {
152             return false;
153         }
154     }
155 
156     @Override
157     public int hashCode() {
158         return id.hashCode();
159     }
160 
161     /* (non-Javadoc)
162      * @see java.lang.Object#toString()
163      */
164     @Override
165     public String toString() {
166         return "id=" + id + ", " + (loginURL != null ? "loginURL=" + loginURL : "");
167     }
168 
169     /**
170      * Returns the URL where users from this realm can login with redirect URL attached.
171      * If this realm has a attribut <code>redirectParameter</code> defined this method returns
172      * a complete login URL with <code>redirectURL</code> properly configured.
173      * @param redirectURL URL where to redirect to after login succeeds.
174      * @return the same as {@link #getLoginURL()} if <code>redirectParameter</code> is undefined for this realm
175      */
176     public String getLoginURL(String redirectURL) {
177         LinkedHashMap<String, String> parameter = new LinkedHashMap<>();
178         String redirect = getRedirectParameter();
179         if (redirect != null && redirectURL != null) {
180             parameter.put(redirect, redirectURL);
181         }
182         String realmParameter = getRealmParameter();
183         if (realmParameter != null) {
184             parameter.put(realmParameter, getID());
185         }
186         if (parameter.isEmpty()) {
187             return getLoginURL();
188         }
189         StringBuilder loginURL = new StringBuilder(getLoginURL());
190         boolean firstParameter = !getLoginURL().contains("?");
191         for (Entry<String, String> entry : parameter.entrySet()) {
192             if (firstParameter) {
193                 loginURL.append('?');
194                 firstParameter = false;
195             } else {
196                 loginURL.append('&');
197             }
198             loginURL.append(entry.getKey()).append('=').append(URLEncoder.encode(entry.getValue(),
199                 StandardCharsets.UTF_8));
200         }
201         return loginURL.toString();
202     }
203 
204     /**
205      * @return the redirectParameter
206      */
207     String getRedirectParameter() {
208         return redirectParameter;
209     }
210 
211     /**
212      * @param redirectParameter the redirectParameter to set
213      */
214     void setRedirectParameter(String redirectParameter) {
215         this.redirectParameter = redirectParameter;
216     }
217 
218     public String getRealmParameter() {
219         return realmParameter;
220     }
221 
222     public void setRealmParameter(String realmParameter) {
223         this.realmParameter = realmParameter;
224     }
225 
226 }