View Javadoc

1   /*
2    * This file is part of NetPisteur.
3    * 
4    * Copyright 2007 : Damien RAUDE-MORVAN
5    * 
6    * NetPisteur is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation; either version 2 of the License, or
9    * (at your option) any later version.
10   *
11   * NetPisteur 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 NetPisteur; if not, write to the Free Software
18   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19   *
20   */
21  package com.drazzib.netpisteur.configuration;
22  
23  import java.io.BufferedInputStream;
24  import java.io.BufferedOutputStream;
25  import java.io.File;
26  import java.io.FileInputStream;
27  import java.io.FileOutputStream;
28  import java.io.IOException;
29  import java.io.WriteAbortedException;
30  import java.util.HashMap;
31  import java.util.Iterator;
32  import java.util.Map;
33  import java.util.Properties;
34  
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  
38  public class ListUtilisateurs {
39  	
40  	private static Log log = LogFactory.getLog(ListUtilisateurs.class);
41  
42  	// /////////////////////////////////////
43  	// access methods for attributes
44  	
45  	/**
46  	 * Represents all the users in the file
47  	 */
48  	private static Map users = new HashMap();
49  
50  	/**
51  	 * writeUtilisateurInfoToDisk()
52  	 * 
53  	 * Method to write datas about the password of the User in a file
54  	 * user.properties
55  	 * 
56  	 * @return success or failure
57  	 */
58  	public static boolean writeUtilisateurInfoToDisk() {
59  	
60  		boolean retour = false;
61  		Utilisateur user;
62  		Properties us = new Properties();
63  		BufferedOutputStream conffile;
64  	
65  		try {
66  			Iterator iterUsers = users.values().iterator();
67  			int i = 0;
68  			while (iterUsers.hasNext())
69  			{
70  				user = (Utilisateur) iterUsers.next();
71  				log.debug("User : "+user);
72  				us.setProperty(i + "_nom", user.getNom());
73  				us.setProperty(i + "_prenom", user.getPrenom());
74  				us.setProperty(i + "_passwd", user.getMotDePasse());
75  				us.setProperty(i + "_separateur", user.getSeparateur());
76  				us.setProperty(i + "_accueil", user.getPage());
77  				i++;
78  			}
79  			
80  			// Lecture de la config
81  			conffile = new BufferedOutputStream(new FileOutputStream(
82  						"conf/user.properties"));
83  			us.store(conffile, "Fichier de configuration des utilisateurs");
84  			
85  			retour = true;
86  	
87  		} catch (WriteAbortedException e) {
88  			log.error("Erreur d'criture du fichier", e);
89  	
90  		} catch (IOException e) {
91  			log.error("Erreur physique de lecture", e);
92  		}
93  	
94  		return (retour);
95  	} // ----------writeUtilisateurInfoToDisk()
96  
97  	// /////////////////////////////////////
98  	// access methods for attributes
99  	
100 	/**
101 	 * readUtilisateurInfoFromDisk()
102 	 * 
103 	 * Method to read datas on the disk, from a file user.dat
104 	 * 
105 	 * @return boolean : success or failure
106 	 */
107 	public static boolean readUtilisateurInfoFromDisk() {
108 	
109 		boolean retour = false;
110 		Utilisateur user;
111 		Properties us = new Properties();
112 		BufferedInputStream conffile;
113 	
114 		try {
115 	
116 			File conf = new File("conf/user.properties");
117 			if (conf.isFile()) {
118 				conffile = new BufferedInputStream(new FileInputStream(
119 						conf));
120 			} else {
121 				conffile = new BufferedInputStream(ListUtilisateurs.class.getResourceAsStream(
122 								"/com/drazzib/netpisteur/conf/user.properties"));
123 			}
124 	
125 			us.load(conffile);
126 	
127 			for (int i = 0; i < us.size(); i++) {
128 				user = new Utilisateur();
129 				user.setNom(us.getProperty(i + "_nom"));
130 				user.setPrenom(us.getProperty(i + "_prenom"));
131 				user.setMotDePasse(us.getProperty(i + "_passwd"));
132 				user.setSeparateur(us.getProperty(i + "_separateur"));
133 				user.setPageAccueil(us.getProperty(i + "_accueil"));
134 				if (user.getNom() != null && user.getPrenom() != null)
135 				{
136 					users.put(user.getNom()+user.getPrenom(), user);
137 				}
138 			}
139 	
140 			retour = true;
141 	
142 		} catch (IOException e) {
143 			log.error("Impossible de lire le fichier de configuration", e);
144 		}
145 	
146 		return (retour);
147 	} // ----------readPasswordFromDisk()
148 
149 	// /////////////////////////////////////
150 	// access methods for attributes
151 	
152 	/**
153 	 * Check if the user already exits in the user base
154 	 * 
155 	 * @return true if the user exits
156 	 */
157 	public static boolean isIn(Utilisateur p_oUtilisateur) {
158 	
159 		boolean isIn = false;
160 		Utilisateur user;
161 		Iterator enumUser = users.values().iterator();
162 	
163 		while (enumUser.hasNext()) {
164 			user = (Utilisateur) (enumUser.next());
165 	
166 			if (user.equals(p_oUtilisateur)) {
167 				isIn = true;
168 			}
169 		}
170 		return isIn;
171 	}
172 	
173 	/**
174 	 * Allows to change a user's password
175 	 * 
176 	 * @param nouveauMotDePasse
177 	 *            the new password
178 	 */
179 	public static void changePasswd(Utilisateur user, String nouveauMotDePasse) {
180 		user.setMotDePasse(nouveauMotDePasse);
181 		writeUtilisateurInfoToDisk();
182 	}
183 
184 	/**
185 	 * Allows to change a user's separator
186 	 * 
187 	 * @param nouveauSep
188 	 *            the new separator
189 	 */
190 	public static void changeSep(Utilisateur user, String nouveauSep) {
191 		user.setSeparateur(nouveauSep);
192 		writeUtilisateurInfoToDisk();
193 	}
194 
195 	/**
196 	 * Allows to change a user's banner page
197 	 * 
198 	 * @param nouveauPage
199 	 *            the new banner page
200 	 */
201 	public static void changePage(Utilisateur user, String nouveauPage) {
202 		user.setPageAccueil(nouveauPage);
203 		writeUtilisateurInfoToDisk();
204 	}
205 
206 	/**
207 	 * Allows to add a user in the file
208 	 */
209 	public static void add(Utilisateur user) {
210 		readUtilisateurInfoFromDisk();
211 		users.put(user.getNom()+user.getPrenom(), user);
212 		log.debug("User list size : "+users.size());
213 		writeUtilisateurInfoToDisk();
214 	}
215 
216 }