comparison flys-client/src/main/java/de/intevation/flys/client/server/auth/plain/Authenticator.java @ 2978:98514ab2c9ba

Implement getting a list of roles from a logged in user flys-client/trunk@4975 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Bjoern Ricks <bjoern.ricks@intevation.de>
date Fri, 13 Jul 2012 09:27:53 +0000
parents 5ba0a6efdf3b
children cc126abafeab
comparison
equal deleted inserted replaced
2977:5161e25392ea 2978:98514ab2c9ba
6 6
7 import java.io.BufferedReader; 7 import java.io.BufferedReader;
8 import java.io.File; 8 import java.io.File;
9 import java.io.FileReader; 9 import java.io.FileReader;
10 import java.io.IOException; 10 import java.io.IOException;
11 import java.util.List;
12 import java.util.ArrayList;
11 13
12 public class Authenticator 14 public class Authenticator
13 implements de.intevation.flys.client.server.auth.Authenticator 15 implements de.intevation.flys.client.server.auth.Authenticator
14 { 16 {
15 public static class Authentication 17 public static class Authentication
16 implements de.intevation.flys.client.server.auth.Authentication 18 implements de.intevation.flys.client.server.auth.Authentication
17 { 19 {
18 protected String user; 20 protected String user;
19 protected String password; 21 protected String password;
22 protected List<String> roles;
20 23
21 public Authentication(String user, String password) { 24 public Authentication(String user, String password, List<String> roles) {
22 this.user = user; 25 this.user = user;
23 this.password = password; 26 this.password = password;
27 this.roles = roles;
24 } 28 }
25 29
26 @Override 30 @Override
27 public boolean isSuccess() { 31 public boolean isSuccess() {
28 return user != null; 32 return user != null;
29 } 33 }
30 34
31 @Override 35 @Override
32 public User getUser() { 36 public User getUser() {
33 return isSuccess() 37 return isSuccess()
34 ? new DefaultUser(user, password, false) 38 ? new DefaultUser(user, password, false, roles)
35 : null; 39 : null;
36 } 40 }
37 } // class Authentication 41 } // class Authentication
38 42
39 public Authenticator() { 43 public Authenticator() {
60 ) 64 )
61 throws AuthenticationException, IOException 65 throws AuthenticationException, IOException
62 { 66 {
63 File file = credentialsFile(); 67 File file = credentialsFile();
64 if (!file.canRead() || !file.isFile()) { 68 if (!file.canRead() || !file.isFile()) {
65 return new Authentication(null, null); 69 return new Authentication(null, null, new ArrayList<String>());
66 } 70 }
67 71
68 BufferedReader reader = 72 BufferedReader reader =
69 new BufferedReader( 73 new BufferedReader(
70 new FileReader(file)); 74 new FileReader(file));
74 if ((line = line.trim()).length() == 0 78 if ((line = line.trim()).length() == 0
75 || line.startsWith("#")) { 79 || line.startsWith("#")) {
76 continue; 80 continue;
77 } 81 }
78 String [] parts = line.split("\\s+"); 82 String [] parts = line.split("\\s+");
79 // TODO: role?
80 if (parts.length < 2) { 83 if (parts.length < 2) {
81 continue; 84 continue;
82 } 85 }
83 if (parts[0].equals(username)) { 86 if (parts[0].equals(username)) {
84 if (parts[1].equals(password)) { 87 if (parts[1].equals(password)) {
85 return new Authentication(username, password); 88 List<String> roles = new ArrayList<String>(parts.length - 2);
89 for (int i = 2; i < parts.length; i++) {
90 roles.add(parts[i]);
91 }
92 return new Authentication(username, password, roles);
86 } 93 }
87 // Stop: user found, wrong password 94 // Stop: user found, wrong password
88 break; 95 break;
89 } 96 }
90 } 97 }
91 } 98 }
92 finally { 99 finally {
93 reader.close(); 100 reader.close();
94 } 101 }
95 return new Authentication(null, null); 102 return new Authentication(null, null, new ArrayList<String>());
96 } 103 }
97 } 104 }
98 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 105 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org