View Javadoc
1   /***************************************************************************
2    * Copyright (C) 2003-2019 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.platform.gadget.services.LoginHistory;
18  
19  import java.util.List;
20  import java.util.Set;
21  
22  import org.exoplatform.container.xml.InitParams;
23  import org.exoplatform.platform.gadget.services.LoginHistory.storage.LoginHistoryStorage;
24  
25  /**
26   * Created by The eXo Platform SARL Author : Tung Vu Minh tungvm@exoplatform.com
27   * Apr 21, 2011 6:19:21 PM
28   */
29  
30  public class LoginHistoryServiceImpl implements LoginHistoryService {
31    private boolean enableLoginHistory = true;
32  
33    private LoginHistoryStorage loginHistoryStorage;
34  
35    public LoginHistoryServiceImpl(LoginHistoryStorage loginHistoryStorage, InitParams params) {
36      this.loginHistoryStorage = loginHistoryStorage;
37      if (params != null && params.containsKey(EXO_AUDIT_LOGIN_ENABLED)) {
38        String enableLoginHistoryString = params.getValueParam(EXO_AUDIT_LOGIN_ENABLED).getValue();
39        enableLoginHistory = enableLoginHistoryString.equalsIgnoreCase("true");
40      }
41    }
42  
43    /**
44     * Get user's last login time
45     */
46    public long getLastLogin(String userId) throws Exception {
47      return loginHistoryStorage.getLastLogin(userId);
48    }
49  
50    /**
51     * Get last logins
52     *
53     * @param numItems
54     * @return List of {numItems} last login entries
55     * @throws Exception
56     */
57    public List<LastLoginBean> getLastLogins(int numItems, String userIdFilter) throws Exception {
58      return loginHistoryStorage.getLastLogins(numItems, userIdFilter);
59    }
60  
61    /**
62     * Add an entry to user login history
63     *
64     * @param userId
65     * @param loginTime
66     * @throws Exception
67     */
68    public void addLoginHistoryEntry(String userId, long loginTime) throws Exception {
69      loginHistoryStorage.addLoginHistoryEntry(userId, loginTime);
70    }
71  
72    /**
73     * Get user login history
74     *
75     * @param userId
76     * @return List of login history entries in range [fromTime..toTime] of user
77     *         {userId}
78     * @throws Exception
79     */
80    public List<LoginHistoryBean> getLoginHistory(String userId, long fromTime, long toTime) throws Exception {
81      return loginHistoryStorage.getLoginHistory(userId, fromTime, toTime);
82    }
83  
84    /**
85     * Get user's login count per days in range [fromDate..toDate]
86     */
87    public List<LoginCounterBean> getLoginCountPerDaysInRange(String userId, long fromDate, long toDate) throws Exception {
88      return getLoginCountPerDaysInRange(userId, fromDate, toDate);
89    }
90  
91    /**
92     * Get user login count per days in given week
93     */
94    public List<LoginCounterBean> getLoginCountPerDaysInWeek(String userId, long week) throws Exception {
95      return loginHistoryStorage.getLoginCountPerDaysInWeek(userId, week);
96    }
97  
98    /**
99     * Get user login count per weeks in given month
100    */
101   public List<LoginCounterBean> getLoginCountPerWeeksInMonths(String userId, long fromMonth, int numOfMonths) throws Exception {
102     return loginHistoryStorage.getLoginCountPerWeeksInMonths(userId, fromMonth, numOfMonths);
103   }
104 
105   /**
106    * Get user login count per months in given year
107    */
108   public List<LoginCounterBean> getLoginCountPerMonthsInYear(String userId, long year) throws Exception {
109     return loginHistoryStorage.getLoginCountPerMonthsInYear(userId, year);
110   }
111 
112   /**
113    * Get the list of all users who are logged after fromTime
114    *
115    * @param fromTime
116    * @return the list of user's name
117    */
118   public Set<String> getLastUsersLogin(long fromTime) throws Exception {
119     return loginHistoryStorage.getLastUsersLogin(fromTime);
120   }
121 
122   public long getBeforeLastLogin(String userId) throws Exception {
123     return loginHistoryStorage.getBeforeLastLogin(userId);
124   }
125 
126   public boolean isEnabled() {
127     return enableLoginHistory;
128   };
129 }