001    // This file is part of AceWiki.
002    // Copyright 2008-2012, AceWiki developers.
003    // 
004    // AceWiki is free software: you can redistribute it and/or modify it under the terms of the GNU
005    // Lesser General Public License as published by the Free Software Foundation, either version 3 of
006    // the License, or (at your option) any later version.
007    // 
008    // AceWiki is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009    // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
010    // Lesser General Public License for more details.
011    // 
012    // You should have received a copy of the GNU Lesser General Public License along with AceWiki. If
013    // not, see http://www.gnu.org/licenses/.
014    
015    package ch.uzh.ifi.attempto.acewiki.core;
016    
017    import java.text.SimpleDateFormat;
018    import java.util.Date;
019    import java.util.HashMap;
020    import java.util.Map;
021    
022    /**
023     * This class stands for the set of registered users for a particular AceWiki instance.
024     * 
025     * @author Tobias Kuhn
026     */
027    public class UserBase {
028            
029            private Ontology ontology;
030            private AceWikiStorage storage;
031            private long idCount = 0;
032            private Map<String, User> userNameMap = new HashMap<String, User>();
033            private Map<Long, User> userIdMap = new HashMap<Long, User>();
034            
035            /**
036             * Creates a new user base for the given ontology.
037             * 
038             * @param ontology The ontology.
039             */
040            UserBase(Ontology ontology, AceWikiStorage storage) {
041                    this.ontology = ontology;
042                    this.storage = storage;
043            }
044            
045            /**
046             * Adds a user to this user base.
047             * 
048             * @param user The user to be added.
049             */
050            void addUser(User user) {
051                    if (user == null) return;
052                    if (user.getId() > idCount) idCount = user.getId();
053                    userNameMap.put(user.getName(), user);
054                    userIdMap.put(user.getId(), user);
055            }
056            
057            /**
058             * Checks whether a user with the respective name exists.
059             * 
060             * @param name The user name.
061             * @return true if the user exists.
062             */
063            public boolean containsUser(String name) {
064                    return userNameMap.containsKey(name);
065            }
066            
067            /**
068             * Returns the user with the given id, or null if no user with this id exists.
069             * 
070             * @param id The user id.
071             * @return The user object.
072             */
073            public User getUser(long id) {
074                    return userIdMap.get(id);
075            }
076            
077            /**
078             * Returns the number of registered users.
079             * 
080             * @return The number of users.
081             */
082            public int getUserCount() {
083                    return userIdMap.size();
084            }
085            
086            /**
087             * Tries to login a user. The user object is returned if the login was successful. Null is
088             * returned otherwise.
089             * 
090             * @param name The name of the user.
091             * @param password The password in plain text.
092             * @return The user object.
093             */
094            public User login(String name, String password) {
095                    User user = userNameMap.get(name);
096                    if (user == null) return null;
097                    if (user.isCorrectPassword(password)) {
098                            user.setUserData("lastlogin", getTimeNow());
099                            user.addToUserDataCounter("logincount", 1);
100                            return user;
101                    }
102                    return null;
103            }
104            
105            /**
106             * Tries to do an auto-login. The user object is returned if the login was successful. Null is
107             * returned otherwise.
108             * 
109             * @param name The name of the user.
110             * @param clientToken The auto-login-token from the client browser.
111             * @return The user object.
112             */
113            public User autoLogin(String name, String clientToken) {
114                    User user = userNameMap.get(name);
115                    if (user == null) return null;
116                    String serverToken = user.getUserData("stayloggedintoken");
117                    if (clientToken.equals(serverToken)) {
118                            user.setUserData("lastlogin", getTimeNow());
119                            user.addToUserDataCounter("logincount", 1);
120                            return user;
121                    }
122                    return null;
123            }
124            
125            /**
126             * Registers a new user. The new user object is returned if the registration was successful.
127             * Null is returned otherwise.
128             * 
129             * @param name The name of the new user.
130             * @param email The email address of the new user.
131             * @param password The password for the new user.
132             * @return The new user object.
133             */
134            public User register(String name, String email, String password) {
135                    if (userNameMap.get(name) != null) return null;
136                    Map<String,String> userdata = new HashMap<String, String>();
137                    userdata.put("email", email);
138                    String now = getTimeNow();
139                    userdata.put("registerdate", now);
140                    userdata.put("lastlogin", now);
141                    userdata.put("logincount", "1");
142                    long id = ++idCount;
143                    User user = User.createUser(id, name, password, userdata, this);
144                    addUser(user);
145                    return user;
146            }
147            
148            /**
149             * Returns the ontology object.
150             * 
151             * @return The ontology.
152             */
153            public Ontology getOntology() {
154                    return ontology;
155            }
156            
157            /**
158             * Returns the storage object.
159             * 
160             * @return The storage.
161             */
162            public AceWikiStorage getStorage() {
163                    return storage;
164            }
165            
166            private String getTimeNow() {
167                    return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
168            }
169    
170    }