1,60 → 1,37 |
package ak.hostadmiral.core.model; |
|
import java.util.*; |
import net.sf.hibernate.*; |
import net.sf.hibernate.type.Type; |
import java.util.Collection; |
import java.util.ArrayList; |
import java.util.Iterator; |
import java.util.Map; |
import java.util.WeakHashMap; |
import java.util.Comparator; |
import java.util.Date; |
|
import ak.hostadmiral.util.ConfigInit; |
import ak.hostadmiral.util.CollectionInfo; |
import ak.hostadmiral.util.HibernateUtil; |
import ak.hostadmiral.util.ModelException; |
import ak.hostadmiral.util.ModelSecurityException; |
import ak.hostadmiral.core.model.store.UserStore; |
|
public class UserManager |
implements |
ConfigInit, |
UserBeforeDeleteListener, |
UserDeletingListener |
{ |
private static UserManager userManager = null; |
private static boolean registered = false; |
private UserStore store; |
|
public static UserManager getInstance() |
{ |
return userManager; |
} |
|
protected static void register() |
{ |
synchronized(UserManager.class) { |
if(registered) return; |
|
registered = true; |
try { |
HibernateUtil.getConfiguration().addResource( |
"ak/hostadmiral/core/model/User.hbm.xml"); |
HibernateUtil.getConfiguration().addResource( |
"ak/hostadmiral/core/model/UserLogin.hbm.xml"); |
HibernateUtil.getConfiguration().addResource( |
"ak/hostadmiral/core/model/PasswordStoreAbstract.hbm.xml"); |
userManager = new UserManager(); |
} |
catch(Exception ex) { |
ex.printStackTrace(); |
throw new RuntimeException(ex.getMessage()); |
} |
} |
} |
|
static { |
register(); |
} |
|
private Collection createdListeners = new ArrayList(); |
private Collection modifiedListeners = new ArrayList(); |
private Collection beforeDeleteListeners = new ArrayList(); |
private Collection deletingListeners = new ArrayList(); |
private Collection deletedListeners = new ArrayList(); |
|
private Map loggedinUsers = new WeakHashMap(); |
|
private UserManager() |
public UserManager() |
throws ModelException |
{ |
addBeforeDeleteListener(this); |
addDeletingListener(this); |
88,15 → 65,8 |
public User get(User editor, Long id) |
throws ModelException |
{ |
User user; |
User user = store.get(id); |
|
try { |
user = (User)HibernateUtil.currentSession().load(User.class, id); |
} |
catch(HibernateException ex) { |
throw new ModelException(ex); |
} |
|
if(!user.viewableBy(editor)) |
throw new ModelSecurityException(); |
|
106,41 → 76,18 |
public boolean loginExists(User editor, User user, String login) |
throws ModelException |
{ |
try { |
if(user.getId() == null) |
return ((Integer)HibernateUtil.currentSession().iterate( |
"select count(*) from User u where login = ?", |
login, Hibernate.STRING) |
.next()).intValue() > 0; |
else |
return ((Integer)HibernateUtil.currentSession().iterate( |
"select count(*) from User u where login = ? and u != ?", |
new Object[] { login, user }, |
new Type[] { Hibernate.STRING, Hibernate.entity(User.class) } ) |
.next()).intValue() > 0; |
} |
catch(HibernateException ex) { |
throw new ModelException(ex); |
} |
return store.loginExists(user, login); |
} |
|
public User findForLogin(String login) |
public User findForLogin(User editor, String login) |
throws ModelException |
{ |
try { |
List list = HibernateUtil.currentSession().find( |
"select u from User u left join fetch u.boss where u.login = ? and u.enabled = ?", |
new Object[] { login, Boolean.TRUE }, |
new Type[] { Hibernate.STRING, Hibernate.BOOLEAN } ); |
User user = store.findForLogin(login); |
|
if(list.size() == 0) |
return null; |
else |
return (User)list.get(0); |
} |
catch(HibernateException ex) { |
throw new ModelException(ex); |
} |
if(user != null && !user.viewableBy(editor)) |
throw new ModelSecurityException(); |
|
return user; |
} |
|
public void save(User editor, User user) |
157,12 → 104,7 |
//user.setModUser(editor); // FIXME: disabled because hb throws exception |
// if user edits itself |
|
try { |
HibernateUtil.currentSession().saveOrUpdate(user); |
} |
catch(HibernateException ex) { |
throw new ModelException(ex); |
} |
store.save(user); |
|
// update user if he is logged in |
for(Iterator i = loggedinUsers.keySet().iterator(); i.hasNext(); ) { |
270,12 → 212,7 |
User oldUser = new User(user); |
|
// delete it |
try { |
HibernateUtil.currentSession().delete(user); |
} |
catch(HibernateException ex) { |
throw new ModelException(ex); |
} |
store.delete(user); |
|
// inform delete listeners |
for(Iterator i = deletedListeners.iterator(); i.hasNext(); ) { |
294,78 → 231,32 |
Integer[] sortingKeys, User editor) |
throws ModelException |
{ |
try { |
if(editor.isSuperuser()) { |
if(info != null) { |
info.init(((Integer)HibernateUtil.currentSession().iterate( |
"select count(*) from User").next()).intValue(), |
pageNumber, rowsPerPage); |
} |
|
return HibernateUtil.pageableList(rowsPerPage, pageNumber, |
"select u from User u left join fetch u.boss" |
+ HibernateUtil.formOrderClause(sortingKeys, sortKeys), null, null); |
} |
else { |
if(info != null) { |
info.init(((Integer)HibernateUtil.currentSession().iterate( |
"select count(*) from User u where u = ? or u.boss = ?", |
new Object[] { editor, editor}, |
new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) } |
).next()).intValue(), |
pageNumber, rowsPerPage); |
} |
|
return HibernateUtil.pageableList(rowsPerPage, pageNumber, |
"select u from User u left join fetch u.boss where u = ? or u.boss = ?" |
+ HibernateUtil.formOrderClause(sortingKeys, sortKeys), |
new Object[] { editor, editor}, |
new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) } ); |
} |
} |
catch(HibernateException ex) { |
throw new ModelException(ex); |
} |
if(editor.isSuperuser()) |
return store.listAllUsers(info, rowsPerPage, pageNumber, sortingKeys); |
else |
return store.listUsers(info, rowsPerPage, pageNumber, sortingKeys, editor); |
} |
|
public boolean areUsersAvailable(User editor) |
throws ModelException |
{ |
try { |
if(editor.isSuperuser()) { |
return true; |
} |
else { |
// FIXME: always true? |
return ((Integer)HibernateUtil.currentSession().iterate( |
"select count(*) from User u where u = ? or u.boss = ?", |
new Object[] { editor, editor}, |
new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) } ) |
.next()).intValue() > 0; |
} |
} |
catch(HibernateException ex) { |
throw new ModelException(ex); |
} |
return true; |
} |
|
public User loginUser(String login, String password, String ip) |
throws ModelException |
{ |
User user = (login == null || password == null) ? null : findForLogin(login); |
User user = (login == null || password == null) |
? null : store.findForLogin(login); |
|
boolean success = (user == null) ? false : user.checkPassword(password); |
UserLogin userLogin = new UserLogin(user, login, new Date(), Boolean.valueOf(success), ip); |
|
// save login information |
try { |
HibernateUtil.currentSession().saveOrUpdate(userLogin); |
} |
catch(HibernateException ex) { |
throw new ModelException(ex); |
} |
store.saveUserLogin(userLogin); |
|
if(success) { |
user = new User(user); // unbind the user from hibernate |
user = new User(user); // unbind the user from store |
loggedinUsers.put(user, Boolean.TRUE); |
return user; |
} |
378,34 → 269,16 |
throws ModelException |
{ |
if(!editor.mayViewAllLogins()) |
{ |
throw new ModelSecurityException(); |
} |
|
try { |
return HibernateUtil.currentSession().find( |
"select l from UserLogin l left join fetch l.user where l.success = ?", |
Boolean.FALSE, Hibernate.BOOLEAN); |
} |
catch(HibernateException ex) { |
throw new ModelException(ex); |
} |
return store.listFailedLogins(); |
} |
|
public Collection userBeforeDelete(User editor, User user, Collection known) |
throws ModelException |
{ |
Collection subusers; |
Collection subusers = store.listSubusers(user); |
|
try { |
subusers = HibernateUtil.currentSession().find( |
"select u from User u where u.boss = ?", |
user, Hibernate.entity(User.class) ); |
} |
catch(HibernateException ex) { |
throw new ModelException(ex); |
} |
|
Collection cascade = new ArrayList(); |
for(Iterator i = subusers.iterator(); i.hasNext(); ) { |
User u = (User)i.next(); |
428,17 → 301,8 |
public void userDeleting(User editor, User user) |
throws ModelException |
{ |
Collection subusers; |
Collection subusers = store.listSubusers(user); |
|
try { |
subusers = HibernateUtil.currentSession().find( |
"select u from User u where u.boss = ?", |
user, Hibernate.entity(User.class) ); |
} |
catch(HibernateException ex) { |
throw new ModelException(ex); |
} |
|
for(Iterator i = subusers.iterator(); i.hasNext(); ) { |
delete(editor, (User)i.next()); |
} |
446,12 → 310,6 |
|
public static final Integer SORT_LOGIN = new Integer(1); |
|
protected static Map sortKeys = new HashMap(); |
|
static { |
sortKeys.put(SORT_LOGIN, "u.login"); |
} |
|
public static final Comparator LOGIN_COMPARATOR = new LoginComparator(); |
public static final Comparator LOGINS_TIME_COMPARATOR = new LoginsTimeComparator(); |
|
508,4 → 366,25 |
return (obj instanceof LoginComparator); |
} |
} |
|
public void init(Map params) |
throws ModelException |
{ |
try { |
userManager = this; |
|
Class c = Class.forName((String)params.get("store")); |
store = (UserStore)c.newInstance(); |
} |
catch(Exception ex) { |
throw new ModelException(ex); |
} |
} |
|
private static UserManager userManager = null; |
|
public static UserManager getInstance() |
{ |
return userManager; |
} |
} |