📄 users.java
字号:
package com.eline.vod.security;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.eline.vod.HttpContext;
import com.eline.vod.security.dao.DAOFactory;
import com.eline.vod.security.dao.UserDAO;
import com.eline.vod.security.model.CreateUserStatus;
import com.eline.vod.security.model.User;
import com.eline.vod.utils.caching.Cache;
public class Users {
public static User getAnonymousUser(boolean formCache) {
final String cacheKey = "MoviesWeb:AnonymousUser";
User user = null;
if (formCache) {
user = (User) Cache.getInstance().get(cacheKey);
}
if (user == null) {
try {
user = DAOFactory.getUserDAO().getAnonymousUser();
} catch (Exception e) {e.printStackTrace();}
if (user != null && user.getUserName() != null && user.getUserId() > 0 && formCache) {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.HOUR, 5);
Cache.getInstance().add(cacheKey, user, cal.getTime(), Cache.NoSlidingExpiration); // five minutes
}
}
return user;
}
/**
* Check out if provided name was previously disallowed.
* @param name
* @return
*/public static boolean disallowedName(String name) {
if (!name.equals(name.trim()))
return false; // username did not being with an alphabetic character.
Pattern pattern = Pattern.compile("[a-zA-Z0-9_]*");
Matcher matcher = pattern.matcher(name);
if (matcher.matches())
return false;
else
return true;
}
public static int create(User user, boolean sendEmail) {
// Check if username is disallowed
System.out.println("STEP:1");
if (disallowedName(user.getUserName()))
return CreateUserStatus.DisallowedUsername;
User userInOut = user;
int status = CreateUserStatus.UnknownFailure;
System.out.println("STEP:2");
try {
UserDAO dao = DAOFactory.getUserDAO();
status = dao.createUser(userInOut);
System.out.println("STEP:3 status=" + status);
if (userInOut != null || status == CreateUserStatus.Created) {
// TODO: We should load default roles from database.
String[] defaultRoles = { "Everyone", "Registered Users" };
for (int i = 0; i < defaultRoles.length; i ++)
Roles.addUserToRole(userInOut.getUserName(), defaultRoles[i]);
}
} catch (Exception e) { e.printStackTrace();}
// TODO: process the emails now
return status;
}
public static User getUser(int userID, String username, boolean isOnline, boolean isCacheable) {
User user = null;
// If the request is not authenticated return
// a new user instance
//
if ((userID == 0) && (username == "Anonymous")) {
user = Users.getAnonymousUser(true);
return user;
}
try {
String lastAction = HttpContext.getCurrent().getRequest().getRequestURL().toString()
+ "?" + HttpContext.getCurrent().getRequest().getQueryString();
user = DAOFactory.getUserDAO().getUser(userID, username, isOnline, lastAction);
} catch (Exception e) {
e.printStackTrace();
}
if (user != null && user.getUserName() != null && user.getUserId() > 0 && isCacheable) {
String userKey = "MoviesWeb:User_";
if (userID > 0)
userKey += "ID-" + userID;
else
userKey += "NAME-" + username;
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.HOUR, 5);
Cache.getInstance().add(userKey, user, cal.getTime(), Cache.NoSlidingExpiration); // five minutes
}
return user;
}
/**
* Validates the User does not alrady exist
*
* @param username
* @param password
* @return CreateUserStatus
* @throws Exception
*/
public static int validateNewUserExists(String username, String mobilePIN) throws Exception {
// 是否非法用户名
if (Users.disallowedName(username))
return CreateUserStatus.DisallowedUsername;
User user;
// 是否已存在用户
if ((user = Users.getUser(0, username, false, false)) != null)
return CreateUserStatus.DuplicateUsername;
// 手机号是否匹配
if (mobilePIN != null) {
user = DAOFactory.getUserDAO().getUserByMobilePIN(mobilePIN);
if (user != null)
return CreateUserStatus.DuplicateMobilePIN;
}
return CreateUserStatus.UnknownFailure;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -