Statistics
| Revision:

gvsig-projects-pool / org.gvsig.vcsgis / trunk / org.gvsig.vcsgis / org.gvsig.vcsgis.lib / org.gvsig.vcsgis.lib.impl / src / main / java / org / gvsig / vcsgis / lib / repository / localdb / requests / AuthenticateRequestLocaldb.java @ 3573

History | View | Annotate | Download (3.88 KB)

1
/*
2
 * gvSIG. Desktop Geographic Information System.
3
 * 
4
 * Copyright (C) 2007-2020 gvSIG Association.
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License 
17
 * along with this program. If not, see <https://www.gnu.org/licenses/>. 
18
 * 
19
 * For any additional information, do not hesitate to contact us
20
 * at info AT gvsig.com, or visit our website www.gvsig.com.
21
 */
22

    
23
package org.gvsig.vcsgis.lib.repository.localdb.requests;
24

    
25
import org.apache.commons.lang3.StringUtils;
26
import org.gvsig.fmap.dal.feature.FeatureSet;
27
import org.gvsig.tools.dispose.DisposeUtils;
28
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_AUTHENTICATE_USER;
29
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_NO_ERROR;
30
import org.gvsig.vcsgis.lib.repository.localdb.VCSGisRepositoryLocaldb;
31
import org.gvsig.vcsgis.lib.repository.localdb.tables.UsersRepoTable;
32
import org.gvsig.vcsgis.lib.repository.localdb.tables.UsersRepoTable.UserRepoRow;
33
import org.gvsig.vcsgis.lib.repository.requests.VCSGisAuthenticateRequest;
34
import org.gvsig.vcsgis.lib.requests.AuthenticateRequestHelper;
35

    
36
/**
37
 *
38
 * @author gvSIG Team
39
 */
40
public class AuthenticateRequestLocaldb extends AbstractRequestLocaldb implements VCSGisAuthenticateRequest {
41
    
42
    public AuthenticateRequestLocaldb(VCSGisRepositoryLocaldb repository) {
43
        super(new AuthenticateRequestHelper(repository));
44
    }
45

    
46
    @Override
47
    public AuthenticateRequestHelper helper() {
48
        return (AuthenticateRequestHelper) super.helper(); 
49
    }
50

    
51
    @Override
52
    public String getUserId() {
53
        return helper().getUserId();
54
    }
55

    
56
    @Override
57
    public String getPassword() {
58
        return helper().getPassword();
59
    }
60

    
61
    @Override
62
    public void setUserId(String id) {
63
        helper().setUserId(id);
64
    }
65

    
66
    @Override
67
    public void setPassword(String password) {
68
        helper().setPassword(password);
69
                
70
    }
71

    
72
    @Override
73
    public int execute() {
74
        FeatureSet.DisposableFeatureSetIterable entities_it=null;
75
        try {
76
            String userCode;
77
            if (!this.getRepository().isAuthenticationRequired()) {
78
                userCode = StringUtils.repeat("0", 38);
79
            } else {
80
                UsersRepoTable usersTable = new UsersRepoTable();
81
                UserRepoRow user = usersTable.getUserById(
82
                        this.getRepository(), 
83
                        helper().getUserId()
84
                );
85
                if( user == null ) {
86
                    return error(ERR_CANT_AUTHENTICATE_USER, "Can't autheticate user '"+helper().getUserId()+"'.");
87
                }
88
                if( !StringUtils.equals(helper().getPassword(), user.getPassword())) {
89
                    return error(ERR_CANT_AUTHENTICATE_USER, "Can't autheticate user '"+helper().getUserId()+"'.");
90
                }
91
                userCode = user.getUserCode();
92
            }
93
            
94
            helper().setAuthenticationToken(
95
                    this.getRepository().getAuthenticationToken(userCode, 8*60) //8 horas
96
            );
97
            helper().setUserCode(userCode);
98
            return error(ERR_NO_ERROR);
99
            
100
        } catch (Exception ex) {
101
            LOGGER.warn("Can't autheticate user '"+helper().getUserId()+"'.",ex);
102
            return error(ERR_CANT_AUTHENTICATE_USER, "Can't autheticate user '"+helper().getUserId()+"'.");
103
        } finally {
104
            DisposeUtils.disposeQuietly(entities_it);
105
        }
106
    }
107

    
108
}