UserBean.java
2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package fi.insomnia.intra.web;
import java.util.Random;
import javax.annotation.Resource;
import javax.annotation.security.DeclareRoles;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.naming.NamingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fi.insomnia.intra.dao.IPagingStatus;
/**
* Session Bean implementation class UserBean
*/
@Stateless
@DeclareRoles(value={"admin","user","manager"})
public class UserBean {
@Resource
private SessionContext ctx;
private static final Logger logger = LoggerFactory.getLogger(UserBean.class);
/**
* Default constructor.
*/
public UserBean() {
// TODO Auto-generated constructor stub
}
public String getUsername()
{
return "User "+ctx.getCallerPrincipal().getName()+"in role admin: " + ctx.isCallerInRole("admin");
}
public String getRemoteUser() {
return ctx.getCallerPrincipal().getName();
}
public boolean ifGrantedRole(String role) {
return ctx.isCallerInRole(role);
}
private static final int GENERATED_PASSWORD_LENGTH = 10;
private static final String VALID_PASSWORD_CHARS="";
private static final String SPECIALCHARS = "!%&|/;:?.,+-_()[]{}#@*";
private static final String LOWERCASECHARS = "abcdefghjklmnpqrstuvwxyz";
private static final String UPPERCASECHARS = "ABCDEFGHJKLMNPQRSTUVWXYZ";
private static final String NUMERALCHARS = "0123456789";
public String createUserWithGeneratedPassword(String username)
{
return "";
}
public static String getRandomString(int count)
{
int visibleChars = VALID_PASSWORD_CHARS.length();
logger.debug("Visble chars: {}",visibleChars);
Random rng = new Random();
StringBuilder strBuild = new StringBuilder();
while(strBuild.length() < count)
{
String selectedStringroup;
switch(rng.nextInt(7))
{
case 0:
case 5:
selectedStringroup = NUMERALCHARS;
break;
case 1:
case 4:
selectedStringroup = UPPERCASECHARS;
break;
case 2:
case 3:
selectedStringroup = LOWERCASECHARS;
break;
case 6:
selectedStringroup = SPECIALCHARS;
break;
default:
selectedStringroup = "";
}
strBuild.append(selectedStringroup.charAt( rng.nextInt(selectedStringroup.length() )));
}
logger.debug("Random string: " + strBuild.toString());
return strBuild.toString();
}
}