001/*
002 *  Copyright 2017 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.core.authentication.token;
017
018import java.util.ArrayList;
019import java.util.Date;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.avalon.framework.parameters.Parameters;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.cocoon.acting.ServiceableAction;
027import org.apache.cocoon.environment.ObjectModelHelper;
028import org.apache.cocoon.environment.Redirector;
029import org.apache.cocoon.environment.Request;
030import org.apache.cocoon.environment.SourceResolver;
031
032import org.ametys.core.authentication.token.AuthenticationTokenManager.Token;
033import org.ametys.core.cocoon.JSonReader;
034import org.ametys.core.util.DateUtils;
035
036/**
037 * Get all authentication tokens in JSON
038 */
039public class GetAuthenticationTokensAction extends ServiceableAction
040{
041    /** The authentication token manager */
042    private AuthenticationTokenManager _authenticationTokenManager;
043
044    /**
045     * Lookup the authentication token manager
046     * 
047     * @return the authentication token manager
048     */
049    protected AuthenticationTokenManager getAuthenticationTokenManager()
050    {
051        if (_authenticationTokenManager == null)
052        {
053            try
054            {
055                _authenticationTokenManager = (AuthenticationTokenManager) manager.lookup(AuthenticationTokenManager.ROLE);
056            }
057            catch (ServiceException e)
058            {
059                throw new IllegalStateException("Cannot get " + AuthenticationTokenManager.ROLE, e);
060            }
061        }
062
063        return _authenticationTokenManager;
064    }
065
066    @Override
067    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
068    {
069        Request request = ObjectModelHelper.getRequest(objectModel);
070
071        Map<String, Object> result = new HashMap<>();
072        List<Map<String, Object>> tokensList = new ArrayList<>();
073
074        List<Token> tokens = getAuthenticationTokenManager().getTokens(AuthenticationTokenManager.USER_TOKEN_TYPE);
075        for (Token token : tokens)
076        {
077            Map<String, Object> infos = new HashMap<>();
078
079            infos.put("id", token.getId());
080            infos.put("description", token.getComment());
081            Date creationDate = token.getCreationDate();
082            infos.put("creationDate", creationDate != null ? DateUtils.dateToString(creationDate) : null);
083            Date endDate = token.getEndDate();
084            infos.put("endDate", endDate != null ? DateUtils.dateToString(endDate) : null);
085            infos.put("nbUsesLeft", token.getNbUsesLeft());
086            infos.put("contexts", token.getContexts());
087            infos.put("comment", token.getComment());
088            tokensList.add(infos);
089        }
090
091        result.put("tokens", tokensList);
092
093        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
094        return EMPTY_MAP;
095    }
096
097}