001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.geoserver.security;
006:
007: import org.acegisecurity.userdetails.UserDetails;
008: import org.acegisecurity.userdetails.UserDetailsService;
009: import org.acegisecurity.userdetails.UsernameNotFoundException;
010: import org.acegisecurity.userdetails.memory.UserMap;
011: import org.acegisecurity.userdetails.memory.UserMapEditor;
012: import org.springframework.dao.DataAccessException;
013: import org.springframework.dao.DataAccessResourceFailureException;
014: import org.vfny.geoserver.global.GeoServer;
015: import org.vfny.geoserver.global.GeoserverDataDirectory;
016:
017: import java.io.BufferedWriter;
018: import java.io.File;
019: import java.io.FileOutputStream;
020: import java.io.FileWriter;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.OutputStream;
024: import java.io.OutputStreamWriter;
025: import java.util.Properties;
026: import java.util.logging.Level;
027: import java.util.logging.Logger;
028:
029: /**
030: * A simple DAO reading the property files
031: *
032: * @author Andrea Aime - TOPP
033: *
034: */
035: public class GeoserverUserDao implements UserDetailsService {
036: /** logger */
037: static Logger LOGGER = org.geotools.util.logging.Logging
038: .getLogger("org.geoserver.security");
039: private UserMap userMap;
040: private PropertyFileWatcher userDefinitionsFile;
041: private GeoServer geoServer;
042:
043: public UserDetails loadUserByUsername(String username)
044: throws UsernameNotFoundException, DataAccessException {
045: checkUserMap();
046:
047: return userMap.getUser(username);
048: }
049:
050: /**
051: * Either loads the default property file on the first access, or reloads it
052: * if it has been modified since last access.
053: *
054: * @throws DataAccessResourceFailureException
055: */
056: private void checkUserMap()
057: throws DataAccessResourceFailureException {
058: InputStream is = null;
059: OutputStream os = null;
060: if ((userMap == null)
061: || ((userDefinitionsFile != null) && userDefinitionsFile
062: .isStale())) {
063: try {
064: if (userDefinitionsFile == null) {
065: File securityDir = GeoserverDataDirectory
066: .findCreateConfigDir("security");
067: File propFile = new File(securityDir,
068: "users.properties");
069:
070: if (!propFile.exists()) {
071: // we're probably dealing with an old data dir, create
072: // the file without
073: // chaning the username and password if possible
074: Properties p = new Properties();
075:
076: if ((geoServer != null)
077: && (geoServer.getAdminUserName() != null)
078: && !geoServer.getAdminUserName().trim()
079: .equals("")) {
080: p.put(geoServer.getAdminUserName(),
081: geoServer.getAdminPassword()
082: + ",ROLE_ADMINISTRATOR");
083: } else {
084: p.put("admin",
085: "geoserver,ROLE_ADMINISTRATOR");
086: }
087:
088: os = new FileOutputStream(propFile);
089: p
090: .store(os,
091: "Format: name=password,ROLE1,...,ROLEN");
092: os.close();
093:
094: // setup a sample service.properties
095: File serviceFile = new File(securityDir,
096: "service.properties");
097: os = new FileOutputStream(serviceFile);
098: is = GeoserverUserDao.class
099: .getResourceAsStream("serviceTemplate.properties");
100: byte[] buffer = new byte[1024];
101: int count = 0;
102: while ((count = is.read(buffer)) > 0) {
103: os.write(buffer, 0, count);
104: }
105: }
106:
107: userDefinitionsFile = new PropertyFileWatcher(
108: propFile);
109: }
110:
111: userMap = new UserMap();
112: UserMapEditor.addUsersFromProperties(userMap,
113: userDefinitionsFile.getProperties());
114: } catch (Exception e) {
115: LOGGER
116: .log(
117: Level.SEVERE,
118: "An error occurred loading user definitions",
119: e);
120: } finally {
121: if (is != null)
122: try {
123: is.close();
124: } catch (IOException ei) { /* nothing to do */
125: }
126: if (os != null)
127: try {
128: os.close();
129: } catch (IOException eo) { /* nothing to do */
130: }
131: }
132: }
133: }
134:
135: public GeoServer getGeoServer() {
136: return geoServer;
137: }
138:
139: public void setGeoServer(GeoServer geoServer) {
140: this.geoServer = geoServer;
141: }
142: }
|