Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 960 → Rev 961

/hostadmiral/trunk/src/ak/hostadmiral/core/model/UserManager.java
0,0 → 1,371
package ak.hostadmiral.core.model;
 
import java.util.*;
import net.sf.hibernate.*;
import net.sf.hibernate.type.Type;
import ak.hostadmiral.util.HibernateUtil;
import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.util.ModelSecurityException;
 
public class UserManager
implements UserBeforeDeleteListener
{
private static UserManager userManager = null;
private static boolean registered = false;
 
public static UserManager getInstance()
{
return userManager;
}
 
protected static void register()
{
synchronized(MailboxManager.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");
 
userManager = new UserManager();
}
catch(Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex.getMessage());
}
}
}
 
static {
register();
}
 
private Collection beforeDeleteListeners = new ArrayList();
private Map loggedinUsers = new WeakHashMap();
 
private UserManager()
{
addBeforeDeleteListener(this);
}
 
public User create(User editor)
throws ModelException
{
if(!allowedToCreate(editor)) throw new ModelSecurityException();
 
return new User();
}
 
public boolean allowedToCreate(User editor)
throws ModelException
{
return User.allowedToCreate(this, editor);
}
 
public User get(User editor, Long id)
throws ModelException
{
User user;
 
try {
user = (User)HibernateUtil.currentSession().load(User.class, id);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
if(!user.viewableBy(editor))
throw new ModelSecurityException();
 
return user;
}
 
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);
}
}
 
public User findForLogin(String login)
throws ModelException
{
try {
List list = HibernateUtil.currentSession().find(
"from User where login = ? and enabled = ?",
new Object[] { login, Boolean.TRUE },
new Type[] { Hibernate.STRING, Hibernate.BOOLEAN } );
 
if(list.size() == 0)
return null;
else
return (User)list.get(0);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public void save(User editor, User user)
throws ModelException
{
if(!user.editableBy(editor) && !user.partEditableBy(editor)
&& !user.mayChangeSuperuser(editor))
{
throw new ModelSecurityException();
}
 
user.setModUser(editor);
 
try {
HibernateUtil.currentSession().saveOrUpdate(user);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
// update user if he is logged in
for(Iterator i = loggedinUsers.keySet().iterator(); i.hasNext(); ) {
User u = (User)i.next();
if(u.equals(user))
u.update(user);
}
}
 
public void addBeforeDeleteListener(UserBeforeDeleteListener listener)
{
beforeDeleteListeners.add(listener);
}
 
public void removeBeforeDeleteListener(UserBeforeDeleteListener listener)
{
beforeDeleteListeners.remove(listener);
}
 
public Collection beforeDelete(User editor, User user, Collection known)
throws ModelException
{
Collection cascade = new ArrayList();
 
for(Iterator i = beforeDeleteListeners.iterator(); i.hasNext(); ) {
UserBeforeDeleteListener listener = (UserBeforeDeleteListener)i.next();
Collection subcascade = listener.userBeforeDelete(editor, user, known);
if(subcascade != null)
cascade.addAll(subcascade);
}
 
return cascade;
}
 
public void delete(User editor, User user)
throws ModelException
{
if(!user.deleteableBy(editor))
throw new ModelSecurityException();
 
try {
HibernateUtil.currentSession().delete(user);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public Collection listUsers(User editor)
throws ModelException
{
try {
if(editor.isSuperuser()) {
return HibernateUtil.currentSession().find("from User");
}
else {
return HibernateUtil.currentSession().find(
"from User u where u = ? or u.boss = ?",
new Object[] { editor, editor},
new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) } );
}
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public boolean areUsersAvailable(User editor)
throws ModelException
{
try {
if(editor.isSuperuser()) {
return true;
}
else {
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);
}
}
 
public User loginUser(String login, String password, String ip)
throws ModelException
{
User user = (login == null || password == null) ? null : 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);
}
 
if(success) {
loggedinUsers.put(user, Boolean.TRUE);
return user;
}
else {
return null; // wrong login or password
}
}
 
public Collection listFailedLogins(User editor)
throws ModelException
{
if(!editor.mayViewAllLogins())
{
throw new ModelSecurityException();
}
 
try {
return HibernateUtil.currentSession().find(
"from UserLogin where success = ?",
Boolean.FALSE, Hibernate.BOOLEAN);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public Collection userBeforeDelete(User editor, User user, Collection known)
throws ModelException
{
Collection subusers;
 
try {
subusers = HibernateUtil.currentSession().find(
"from User where 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();
if(u.viewableBy(editor)) {
if(u.deleteableBy(editor))
cascade.add(new CascadeDeleteElement(u, CascadeDeleteElement.DELETE,
this.beforeDelete(editor, u, known)));
else
cascade.add(new CascadeDeleteElement(u, CascadeDeleteElement.FORBIDDEN, null));
}
else {
cascade.add(new CascadeDeleteElement(User.createLimitedCopy(u),
CascadeDeleteElement.FORBIDDEN, null));
}
}
 
return cascade;
}
 
public static final Comparator LOGIN_COMPARATOR = new LoginComparator();
public static final Comparator LOGINS_TIME_COMPARATOR = new LoginsTimeComparator();
 
private static class LoginComparator
implements Comparator
{
public int compare(Object o1, Object o2)
{
if(!(o1 instanceof User) || !(o2 instanceof User))
throw new ClassCastException("not a User");
 
User a1 = (User)o1;
User a2 = (User)o2;
 
if(a1 == null && a2 == null)
return 0;
else if(a1 == null && a2 != null)
return -1;
else if(a1 != null && a2 == null)
return 1;
else
return a1.getLogin().compareToIgnoreCase(a2.getLogin());
}
 
public boolean equals(Object obj)
{
return (obj instanceof LoginComparator);
}
}
 
private static class LoginsTimeComparator
implements Comparator
{
public int compare(Object o1, Object o2)
{
if(!(o1 instanceof UserLogin) || !(o2 instanceof UserLogin))
throw new ClassCastException("not a UserLogin");
 
UserLogin a1 = (UserLogin)o1;
UserLogin a2 = (UserLogin)o2;
 
if(a1 == null && a2 == null)
return 0;
else if(a1 == null && a2 != null)
return -1;
else if(a1 != null && a2 == null)
return 1;
else
return a1.getLoginTime().compareTo(a2.getLoginTime());
}
 
public boolean equals(Object obj)
{
return (obj instanceof LoginComparator);
}
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/InetDomainManager.java
0,0 → 1,278
package ak.hostadmiral.core.model;
 
import java.util.*;
import net.sf.hibernate.*;
import net.sf.hibernate.type.Type;
import ak.hostadmiral.util.HibernateUtil;
import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.util.ModelSecurityException;
 
public class InetDomainManager
implements UserBeforeDeleteListener
{
private static InetDomainManager inetDomainManager = null;
private static boolean registered = false;
 
public static InetDomainManager getInstance()
{
return inetDomainManager;
}
 
protected static void register()
{
synchronized(InetDomainManager.class) {
if(registered) return;
 
registered = true;
try {
HibernateUtil.getConfiguration().addResource(
"ak/hostadmiral/core/model/InetDomain.hbm.xml");
 
inetDomainManager = new InetDomainManager();
}
catch(Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex.getMessage());
}
}
}
 
static {
register();
}
 
private Collection beforeDeleteListeners = new ArrayList();
 
private InetDomainManager()
{
UserManager.getInstance().addBeforeDeleteListener(this);
}
 
public InetDomain create(User editor)
throws ModelException
{
if(!allowedToCreate(editor)) throw new ModelSecurityException();
 
return new InetDomain();
}
 
public boolean allowedToCreate(User editor)
throws ModelException
{
return InetDomain.allowedToCreate(this, editor);
}
 
public InetDomain get(User editor, Long id)
throws ModelException
{
InetDomain domain;
 
try {
domain = (InetDomain)HibernateUtil.currentSession().load(
InetDomain.class, id);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
if(!domain.viewableBy(editor))
throw new ModelSecurityException();
 
return domain;
}
 
public boolean nameExists(User editor, InetDomain domain, String name)
throws ModelException
{
try {
if(domain.getId() == null)
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from InetDomain d where name = ?",
name, Hibernate.STRING)
.next()).intValue() > 0;
else
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from InetDomain d where name = ? and d != ?",
new Object[] { name, domain },
new Type[] { Hibernate.STRING, Hibernate.entity(InetDomain.class) } )
.next()).intValue() > 0;
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
protected InetDomain findForName(String name)
throws ModelException
{
try {
List list = HibernateUtil.currentSession().find(
"from InetDomain where name=?", name, Hibernate.STRING);
 
if(list.size() == 0)
return null;
else
return (InetDomain)list.get(0);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public void save(User editor, InetDomain domain)
throws ModelException
{
if(!domain.editableBy(editor))
throw new ModelSecurityException();
 
domain.setModUser(editor);
 
try {
HibernateUtil.currentSession().saveOrUpdate(domain);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public void addBeforeDeleteListener(SystemUserBeforeDeleteListener listener)
{
beforeDeleteListeners.add(listener);
}
 
public void removeBeforeDeleteListener(SystemUserBeforeDeleteListener listener)
{
beforeDeleteListeners.remove(listener);
}
 
public Collection beforeDelete(User editor, InetDomain domain, Collection known)
throws ModelException
{
Collection cascade = new ArrayList();
 
for(Iterator i = beforeDeleteListeners.iterator(); i.hasNext(); ) {
InetDomainBeforeDeleteListener listener = (InetDomainBeforeDeleteListener)i.next();
Collection subcascade = listener.inetDomainBeforeDelete(editor, domain, known);
if(subcascade != null)
cascade.addAll(subcascade);
}
 
return cascade;
}
 
public void delete(User editor, InetDomain domain)
throws ModelException
{
if(!domain.deleteableBy(editor))
throw new ModelSecurityException();
 
try {
 
HibernateUtil.currentSession().delete(domain);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public Collection listInetDomains(User editor)
throws ModelException
{
try {
if(editor.isSuperuser())
return HibernateUtil.currentSession().find("from InetDomain");
else
return HibernateUtil.currentSession().find(
"from InetDomain where owner=?", editor, Hibernate.entity(User.class));
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public boolean areInetDomainsAvailable(User editor)
throws ModelException
{
try {
if(editor.isSuperuser())
return true;
else
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from InetDomain where owner=?",
editor, Hibernate.entity(User.class)).next()).intValue() > 0;
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public Collection userBeforeDelete(User editor, User user, Collection known)
throws ModelException
{
Collection domains;
 
try {
domains = HibernateUtil.currentSession().find(
"from InetDomain where owner = ?",
user, Hibernate.entity(User.class) );
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
Collection cascade = new ArrayList();
for(Iterator i = domains.iterator(); i.hasNext(); ) {
InetDomain d = (InetDomain)i.next();
if(d.viewableBy(editor)) {
if(d.deleteableBy(editor))
cascade.add(new CascadeDeleteElement(d, CascadeDeleteElement.DELETE,
this.beforeDelete(editor, d, known)));
else
cascade.add(new CascadeDeleteElement(d, CascadeDeleteElement.FORBIDDEN, null));
}
else {
cascade.add(new CascadeDeleteElement(InetDomain.createLimitedCopy(d),
CascadeDeleteElement.FORBIDDEN, null));
}
}
 
return cascade;
}
 
public static final Comparator NAME_COMPARATOR = new NameComparator();
 
private static class NameComparator
implements Comparator
{
public int compare(Object o1, Object o2)
{
if(!(o1 instanceof InetDomain) || !(o2 instanceof InetDomain))
throw new ClassCastException("not a InetDomain");
 
InetDomain a1 = (InetDomain)o1;
InetDomain a2 = (InetDomain)o2;
 
if(a1 == null && a2 == null)
return 0;
else if(a1 == null && a2 != null)
return -1;
else if(a1 != null && a2 == null)
return 1;
else
return a1.getName().compareToIgnoreCase(a2.getName());
}
 
public boolean equals(Object obj)
{
return (obj instanceof NameComparator);
}
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/MailboxManager.java
0,0 → 1,339
package ak.hostadmiral.core.model;
 
import java.util.*;
import net.sf.hibernate.*;
import net.sf.hibernate.type.Type;
import ak.hostadmiral.util.HibernateUtil;
import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.util.ModelSecurityException;
 
public class MailboxManager
implements
UserBeforeDeleteListener,
SystemUserBeforeDeleteListener,
InetDomainBeforeDeleteListener
{
private static MailboxManager mailboxManager = null;
private static boolean registered = false;
 
public static MailboxManager getInstance()
{
return mailboxManager;
}
 
protected static void register()
{
synchronized(MailboxManager.class) {
if(registered) return;
 
registered = true;
try {
HibernateUtil.getConfiguration().addResource(
"ak/hostadmiral/core/model/Mailbox.hbm.xml");
 
mailboxManager = new MailboxManager();
}
catch(Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex.getMessage());
}
}
}
 
static {
register();
}
 
private Collection beforeDeleteListeners = new ArrayList();
 
private MailboxManager()
{
UserManager.getInstance().addBeforeDeleteListener(this);
SystemUserManager.getInstance().addBeforeDeleteListener(this);
InetDomainManager.getInstance().addBeforeDeleteListener(this);
}
 
public Mailbox create(User editor)
throws ModelException
{
if(!allowedToCreate(editor)) throw new ModelSecurityException();
 
return new Mailbox();
}
 
public boolean allowedToCreate(User editor)
throws ModelException
{
return Mailbox.allowedToCreate(this, editor);
}
 
public Mailbox get(User editor, Long id)
throws ModelException
{
Mailbox mailbox;
 
try {
mailbox = (Mailbox)HibernateUtil.currentSession().load(Mailbox.class, id);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
if(!mailbox.viewableBy(editor))
throw new ModelSecurityException();
 
return mailbox;
}
 
public boolean loginExists(User editor, Mailbox mailbox, String login)
throws ModelException
{
if(mailbox.getDomain() == null)
throw new ModelException("Cannot check unique login for mailbox without domain");
 
try {
if(mailbox.getId() == null)
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from Mailbox where login = ? and domain = ?",
new Object[] { login, mailbox.getDomain() },
new Type[] { Hibernate.STRING, Hibernate.entity(InetDomain.class) } )
.next()).intValue() > 0;
else
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from Mailbox b where login = ? and domain = ? and b != ?",
new Object[] { login, mailbox.getDomain(), mailbox },
new Type[] { Hibernate.STRING, Hibernate.entity(InetDomain.class),
Hibernate.entity(Mailbox.class) } )
.next()).intValue() > 0;
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
protected Mailbox findForLogin(String login)
throws ModelException
{
try {
List list = HibernateUtil.currentSession().find(
"from Mailbox where login=?", login, Hibernate.STRING);
 
if(list.size() == 0)
return null;
else
return (Mailbox)list.get(0);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public void save(User editor, Mailbox mailbox)
throws ModelException
{
if(!mailbox.editableBy(editor))
throw new ModelSecurityException();
 
mailbox.setModUser(editor);
 
try {
HibernateUtil.currentSession().saveOrUpdate(mailbox);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public void addBeforeDeleteListener(MailboxBeforeDeleteListener listener)
{
beforeDeleteListeners.add(listener);
}
 
public void removeBeforeDeleteListener(MailboxBeforeDeleteListener listener)
{
beforeDeleteListeners.remove(listener);
}
 
public Collection beforeDelete(User editor, Mailbox mailbox, Collection known)
throws ModelException
{
Collection cascade = new ArrayList();
 
for(Iterator i = beforeDeleteListeners.iterator(); i.hasNext(); ) {
MailboxBeforeDeleteListener listener = (MailboxBeforeDeleteListener)i.next();
Collection subcascade = listener.mailboxBeforeDelete(editor, mailbox, known);
if(subcascade != null)
cascade.addAll(subcascade);
}
 
return cascade;
}
 
public void delete(User editor, Mailbox mailbox)
throws ModelException
{
if(!mailbox.deleteableBy(editor))
throw new ModelSecurityException();
 
try {
HibernateUtil.currentSession().delete(mailbox);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public Collection listMailboxes(User editor)
throws ModelException
{
try {
if(editor.isSuperuser())
return HibernateUtil.currentSession().find("from Mailbox");
else
return HibernateUtil.currentSession().find(
"select mb from Mailbox mb left join mb.domain as d"
+ " where d.owner=? or mb.owner=?",
new Object[] { editor, editor },
new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) } );
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public boolean areMailboxesAvailable(User editor)
throws ModelException
{
try {
if(editor.isSuperuser()
|| InetDomainManager.getInstance().areInetDomainsAvailable(editor))
{
return true;
}
else {
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from Mailbox mb left join mb.domain as d"
+ " where d.owner=? or mb.owner=?",
new Object[] { editor, editor },
new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) })
.next()).intValue() > 0;
}
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public Collection userBeforeDelete(User editor, User user, Collection known)
throws ModelException
{
Collection mailboxes;
 
try {
mailboxes = HibernateUtil.currentSession().find(
"from Mailbox where owner = ?",
user, Hibernate.entity(User.class) );
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
return iterateBeforeDelete(editor, mailboxes, known);
}
 
public Collection inetDomainBeforeDelete(User editor, InetDomain domain, Collection known)
throws ModelException
{
Collection mailboxes;
 
try {
mailboxes = HibernateUtil.currentSession().find(
"from Mailbox where domain = ?",
domain, Hibernate.entity(InetDomain.class) );
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
return iterateBeforeDelete(editor, mailboxes, known);
}
 
public Collection systemUserBeforeDelete(User editor, SystemUser user, Collection known)
throws ModelException
{
Collection mailboxes;
 
try {
mailboxes = HibernateUtil.currentSession().find(
"from Mailbox where systemUser = ?",
user, Hibernate.entity(SystemUser.class) );
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
return iterateBeforeDelete(editor, mailboxes, known);
}
 
private Collection iterateBeforeDelete(User editor, Collection mailboxes, Collection known)
throws ModelException
{
Collection cascade = new ArrayList();
for(Iterator i = mailboxes.iterator(); i.hasNext(); ) {
Mailbox mailbox = (Mailbox)i.next();
if(mailbox.viewableBy(editor)) {
if(mailbox.deleteableBy(editor))
cascade.add(new CascadeDeleteElement(mailbox, CascadeDeleteElement.DELETE,
this.beforeDelete(editor, mailbox, known)));
else
cascade.add(new CascadeDeleteElement(mailbox, CascadeDeleteElement.FORBIDDEN,
null));
}
else {
cascade.add(new CascadeDeleteElement(Mailbox.createLimitedCopy(mailbox),
CascadeDeleteElement.FORBIDDEN, null));
}
}
 
return cascade;
}
 
public static final Comparator LOGIN_COMPARATOR = new LoginComparator();
 
private static class LoginComparator
implements Comparator
{
public int compare(Object o1, Object o2)
{
if(!(o1 instanceof Mailbox) || !(o2 instanceof Mailbox))
throw new ClassCastException("not a Mailbox");
 
Mailbox a1 = (Mailbox)o1;
Mailbox a2 = (Mailbox)o2;
 
if(a1 == null && a2 == null)
return 0;
else if(a1 == null && a2 != null)
return -1;
else if(a1 != null && a2 == null)
return 1;
else
return a1.getLogin().compareToIgnoreCase(a2.getLogin());
}
 
public boolean equals(Object obj)
{
return (obj instanceof LoginComparator);
}
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/MailAliasManager.java
0,0 → 1,231
package ak.hostadmiral.core.model;
 
import java.util.*;
import net.sf.hibernate.*;
import net.sf.hibernate.type.Type;
import ak.hostadmiral.util.HibernateUtil;
import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.util.ModelSecurityException;
 
public class MailAliasManager
{
private static boolean registered = false;
protected static void register()
{
synchronized(MailAliasManager.class) {
if(registered) return;
 
registered = true;
try {
HibernateUtil.getConfiguration().addResource(
"ak/hostadmiral/core/model/MailAlias.hbm.xml");
}
catch(Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex.getMessage());
}
}
}
 
static {
register();
}
 
private MailAliasManager()
{
}
 
public MailAlias create(User editor)
throws ModelException
{
if(!allowedToCreate(editor)) throw new ModelSecurityException();
 
MailAlias alias = new MailAlias();
alias.setDestinations(new ArrayList());
return alias;
}
 
public boolean allowedToCreate(User editor)
throws ModelException
{
return MailAlias.allowedToCreate(this, editor);
}
 
public MailAlias get(User editor, Long id)
throws ModelException
{
MailAlias alias;
 
try {
alias = (MailAlias)HibernateUtil.currentSession().load(MailAlias.class, id);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
if(!alias.viewableBy(editor))
throw new ModelSecurityException();
 
return alias;
}
 
public boolean addressExists(User editor, MailAlias alias, String address)
throws ModelException
{
if(alias.getDomain() == null)
throw new ModelException("Cannot check unique address for mail alias without domain");
 
try {
if(alias.getId() == null)
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from MailAlias where address = ? and domain = ?",
new Object[] { address, alias.getDomain() },
new Type[] { Hibernate.STRING, Hibernate.entity(InetDomain.class) } )
.next()).intValue() > 0;
else
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from MailAlias a where address = ? and domain = ? and a != ?",
new Object[] { address, alias.getDomain(), alias },
new Type[] { Hibernate.STRING, Hibernate.entity(InetDomain.class),
Hibernate.entity(MailAlias.class) } )
.next()).intValue() > 0;
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
protected MailAlias findForName(String name)
throws ModelException
{
try {
List list = HibernateUtil.currentSession().find(
"from MailAlias where name=?", name, Hibernate.STRING);
 
if(list.size() == 0)
return null;
else
return (MailAlias)list.get(0);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public void save(User editor, MailAlias mailAlias)
throws ModelException
{
if(!mailAlias.editableBy(editor))
throw new ModelSecurityException();
 
mailAlias.setModUser(editor);
 
try {
HibernateUtil.currentSession().saveOrUpdate(mailAlias);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public void delete(User editor, MailAlias mailAlias)
throws ModelException
{
if(!mailAlias.deleteableBy(editor))
throw new ModelSecurityException();
 
try {
HibernateUtil.currentSession().delete(mailAlias);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public Collection listMailAliases(User editor)
throws ModelException
{
try {
if(editor.isSuperuser())
return HibernateUtil.currentSession().find("from MailAlias");
else
return HibernateUtil.currentSession().find(
"select a from MailAlias a left join a.domain as d"
+ " where d.owner=? or a.owner=?",
new Object[] { editor, editor },
new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) } );
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public boolean areMailAliasesAvailable(User editor)
throws ModelException
{
try {
if(editor.isSuperuser()
|| InetDomainManager.getInstance().areInetDomainsAvailable(editor))
{
return true;
}
else {
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from MailAlias a left join a.domain as d"
+ " where d.owner=? or a.owner=?",
new Object[] { editor, editor },
new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) })
.next()).intValue() > 0;
}
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
private static MailAliasManager mailAliasManager = null;
 
public static MailAliasManager getInstance()
{
if(mailAliasManager == null)
mailAliasManager = new MailAliasManager();
 
return mailAliasManager;
}
 
public static final Comparator ADDRESS_COMPARATOR = new AddressComparator();
 
private static class AddressComparator
implements Comparator
{
public int compare(Object o1, Object o2)
{
if(!(o1 instanceof MailAlias) || !(o2 instanceof MailAlias))
throw new ClassCastException("not a MailAlias");
 
MailAlias a1 = (MailAlias)o1;
MailAlias a2 = (MailAlias)o2;
 
if(a1 == null && a2 == null)
return 0;
else if(a1 == null && a2 != null)
return -1;
else if(a1 != null && a2 == null)
return 1;
else
return a1.getAddress().compareToIgnoreCase(a2.getAddress());
}
 
public boolean equals(Object obj)
{
return (obj instanceof AddressComparator);
}
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/MailAliasDestinationManager.java
0,0 → 1,162
package ak.hostadmiral.core.model;
 
import java.util.*;
import net.sf.hibernate.*;
import ak.hostadmiral.util.HibernateUtil;
import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.util.ModelSecurityException;
 
public class MailAliasDestinationManager
{
private static boolean registered = false;
protected static void register()
{
synchronized(MailAliasDestinationManager.class) {
if(registered) return;
 
registered = true;
try {
HibernateUtil.getConfiguration().addResource(
"ak/hostadmiral/core/model/MailAliasDestination.hbm.xml");
}
catch(Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex.getMessage());
}
}
}
 
static {
register();
}
 
private MailAliasDestinationManager()
{
}
 
public MailAliasDestination create(User editor)
throws ModelException
{
if(!allowedToCreate(editor)) throw new ModelSecurityException();
 
return new MailAliasDestination();
}
 
public boolean allowedToCreate(User editor)
throws ModelException
{
return MailAliasDestination.allowedToCreate(this, editor);
}
 
public MailAliasDestination get(User editor, Long id)
throws ModelException
{
MailAliasDestination dest;
 
try {
dest = (MailAliasDestination)HibernateUtil.currentSession()
.load(MailAliasDestination.class, id);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
if(!dest.viewableBy(editor))
throw new ModelSecurityException();
 
return dest;
}
 
public void save(User editor, MailAliasDestination mailAliasDestination)
throws ModelException
{
if(!mailAliasDestination.editableBy(editor))
throw new ModelSecurityException();
 
mailAliasDestination.setModUser(editor);
// FIXME: the mod_user is not set when changing a destination as element of collection
 
try {
HibernateUtil.currentSession().saveOrUpdate(mailAliasDestination);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public void delete(User editor, MailAliasDestination mailAliasDestination)
throws ModelException
{
if(!mailAliasDestination.deleteableBy(editor))
throw new ModelSecurityException();
 
try {
HibernateUtil.currentSession().delete(mailAliasDestination);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public Collection listMailAliasesDestination(MailAlias alias)
throws ModelException
{
try {
return HibernateUtil.currentSession().find(
"from MailAliasDestination where alias=?",
alias, Hibernate.entity(MailAlias.class));
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public boolean areMailAliasesDestinationsAvailable(User editor)
throws ModelException
{
return true;
}
 
private static MailAliasDestinationManager mailAliasDestinationManager = null;
 
public static MailAliasDestinationManager getInstance()
{
if(mailAliasDestinationManager == null)
mailAliasDestinationManager = new MailAliasDestinationManager();
 
return mailAliasDestinationManager;
}
 
public static final Comparator EMAIL_COMPARATOR = new EmailComparator();
 
private static class EmailComparator
implements Comparator
{
public int compare(Object o1, Object o2)
{
if(!(o1 instanceof MailAliasDestination) || !(o2 instanceof MailAliasDestination))
throw new ClassCastException("not a MailAliasDestination");
 
MailAliasDestination a1 = (MailAliasDestination)o1;
MailAliasDestination a2 = (MailAliasDestination)o2;
 
if(a1 == null && a2 == null)
return 0;
else if(a1 == null && a2 != null)
return -1;
else if(a1 != null && a2 == null)
return 1;
else
return a1.getEmail().compareToIgnoreCase(a2.getEmail());
}
 
public boolean equals(Object obj)
{
return (obj instanceof EmailComparator);
}
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/SystemUserManager.java
0,0 → 1,347
package ak.hostadmiral.core.model;
 
import java.util.*;
import net.sf.hibernate.*;
import net.sf.hibernate.type.Type;
import ak.hostadmiral.util.HibernateUtil;
import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.util.ModelSecurityException;
 
public class SystemUserManager
implements UserBeforeDeleteListener
{
private static SystemUserManager systemUserManager = null;
private static boolean registered = false;
 
public static SystemUserManager getInstance()
{
return systemUserManager;
}
 
protected static void register()
{
synchronized(SystemUserManager.class) {
if(registered) return;
 
registered = true;
try {
HibernateUtil.getConfiguration().addResource(
"ak/hostadmiral/core/model/SystemUser.hbm.xml");
 
systemUserManager = new SystemUserManager();
}
catch(Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex.getMessage());
}
}
}
 
static {
register();
}
 
private Collection beforeDeleteListeners = new ArrayList();
 
private SystemUserManager()
{
UserManager.getInstance().addBeforeDeleteListener(this);
}
 
public SystemUser create(User editor)
throws ModelException
{
if(!allowedToCreate(editor)) throw new ModelSecurityException();
 
return new SystemUser();
}
 
public boolean allowedToCreate(User editor)
throws ModelException
{
return SystemUser.allowedToCreate(this, editor);
}
 
public SystemUser get(User editor, Long id)
throws ModelException
{
SystemUser user;
 
try {
user = (SystemUser)HibernateUtil.currentSession().load(SystemUser.class, id);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
if(!user.viewableBy(editor))
throw new ModelSecurityException();
 
return user;
}
 
public boolean nameExists(User editor, SystemUser user, String name)
throws ModelException
{
try {
if(user.getId() == null)
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from SystemUser u where name = ?",
name, Hibernate.STRING)
.next()).intValue() > 0;
else
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from SystemUser u where name = ? and u != ?",
new Object[] { name, user },
new Type[] { Hibernate.STRING, Hibernate.entity(SystemUser.class) } )
.next()).intValue() > 0;
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public boolean uidExists(User editor, SystemUser user, Integer uid)
throws ModelException
{
try {
if(user.getId() == null)
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from SystemUser u where uid = ?",
uid, Hibernate.INTEGER)
.next()).intValue() > 0;
else
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from SystemUser u where uid = ? and u != ?",
new Object[] { uid, user },
new Type[] { Hibernate.INTEGER, Hibernate.entity(SystemUser.class) } )
.next()).intValue() > 0;
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
protected SystemUser findForName(String name)
throws ModelException
{
try {
List list = HibernateUtil.currentSession().find(
"from SystemUser where name=?", name, Hibernate.STRING);
 
if(list.size() == 0)
return null;
else
return (SystemUser)list.get(0);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
protected SystemUser findForUid(Integer uid)
throws ModelException
{
try {
List list = HibernateUtil.currentSession().find(
"from SystemUser where uid=?", uid, Hibernate.INTEGER);
 
if(list.size() == 0)
return null;
else
return (SystemUser)list.get(0);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public void save(User editor, SystemUser systemUser)
throws ModelException
{
if(!systemUser.editableBy(editor))
throw new ModelSecurityException();
 
systemUser.setModUser(editor);
 
try {
HibernateUtil.currentSession().saveOrUpdate(systemUser);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public void addBeforeDeleteListener(SystemUserBeforeDeleteListener listener)
{
beforeDeleteListeners.add(listener);
}
 
public void removeBeforeDeleteListener(SystemUserBeforeDeleteListener listener)
{
beforeDeleteListeners.remove(listener);
}
 
public Collection beforeDelete(User editor, SystemUser user, Collection known)
throws ModelException
{
Collection cascade = new ArrayList();
 
for(Iterator i = beforeDeleteListeners.iterator(); i.hasNext(); ) {
SystemUserBeforeDeleteListener listener = (SystemUserBeforeDeleteListener)i.next();
Collection subcascade = listener.systemUserBeforeDelete(editor, user, known);
if(subcascade != null)
cascade.addAll(subcascade);
}
 
return cascade;
}
 
public void delete(User editor, SystemUser systemUser)
throws ModelException
{
if(!systemUser.deleteableBy(editor))
throw new ModelSecurityException();
 
try {
HibernateUtil.currentSession().delete(systemUser);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public Collection listSystemUsers(User editor)
throws ModelException
{
try {
if(editor.isSuperuser()) {
return HibernateUtil.currentSession().find("from SystemUser");
}
else {
return HibernateUtil.currentSession().find(
"select u from SystemUser u left join u.owner o where o is null or o=?",
editor, Hibernate.entity(User.class));
}
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public boolean areSystemUsersAvailable(User editor)
throws ModelException
{
try {
if(editor.isSuperuser())
return true;
else
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from SystemUser u left join u.owner o where o is null or o=?",
editor, Hibernate.entity(User.class)).next()).intValue() > 0;
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public Collection userBeforeDelete(User editor, User user, Collection known)
throws ModelException
{
Collection systemUsers;
 
try {
systemUsers = HibernateUtil.currentSession().find(
"from SystemUser where owner = ?",
user, Hibernate.entity(User.class) );
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
Collection cascade = new ArrayList();
for(Iterator i = systemUsers.iterator(); i.hasNext(); ) {
SystemUser u = (SystemUser)i.next();
if(u.viewableBy(editor)) {
if(u.deleteableBy(editor))
cascade.add(new CascadeDeleteElement(u, CascadeDeleteElement.DELETE,
this.beforeDelete(editor, u, known)));
else
cascade.add(new CascadeDeleteElement(u, CascadeDeleteElement.FORBIDDEN, null));
}
else {
cascade.add(new CascadeDeleteElement(SystemUser.createLimitedCopy(u),
CascadeDeleteElement.FORBIDDEN, null));
}
}
 
return cascade;
}
 
public static final Comparator UID_COMPARATOR = new UidComparator();
public static final Comparator NAME_COMPARATOR = new NameComparator();
 
private static class UidComparator
implements Comparator
{
public int compare(Object o1, Object o2)
{
if(!(o1 instanceof SystemUser) || !(o2 instanceof SystemUser))
throw new ClassCastException("not a SystemUser");
 
SystemUser a1 = (SystemUser)o1;
SystemUser a2 = (SystemUser)o2;
 
if(a1 == null && a2 == null)
return 0;
else if(a1 == null && a2 != null)
return -1;
else if(a1 != null && a2 == null)
return 1;
else
return a1.getUid().compareTo(a2.getUid());
}
 
public boolean equals(Object obj)
{
return (obj instanceof UidComparator);
}
}
 
private static class NameComparator
implements Comparator
{
public int compare(Object o1, Object o2)
{
if(!(o1 instanceof SystemUser) || !(o2 instanceof SystemUser))
throw new ClassCastException("not a SystemUser");
 
SystemUser a1 = (SystemUser)o1;
SystemUser a2 = (SystemUser)o2;
 
if(a1 == null && a2 == null)
return 0;
else if(a1 == null && a2 != null)
return -1;
else if(a1 != null && a2 == null)
return 1;
else
return a1.getName().compareToIgnoreCase(a2.getName());
}
 
public boolean equals(Object obj)
{
return (obj instanceof NameComparator);
}
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/User.java
0,0 → 1,297
package ak.hostadmiral.core.model;
 
import java.util.Collection;
import java.util.Collections;
import java.util.Locale;
import java.util.StringTokenizer;
 
import ak.hostadmiral.util.Digest;
import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.util.ModelSecurityException;
 
/**
*
* @hibernate.class table="users"
*/
public class User
extends GeneralModelObject
{
private String login;
private String password;
private User boss;
private Boolean superuser;
private Locale locale = Locale.getDefault();
private Collection loginHistory;
 
protected User()
{
}
 
/**
*
* @hibernate.property
*/
public String getLogin()
{
return login;
}
 
protected void setLogin(String login)
{
this.login = login;
}
 
public void setLogin(User editor, String login)
throws ModelException
{
if(!editableBy(editor))
throw new ModelSecurityException();
 
this.login = login;
}
 
/**
*
* @hibernate.property
*/
protected String getPassword()
{
return password;
}
 
protected void setPassword(String password)
{
this.password = password;
}
 
public void setPassword(User editor, String password)
throws ModelException
{
if(!partEditableBy(editor))
throw new ModelSecurityException();
 
if(password == null)
throw new NullPointerException("Null password");
 
this.password = Digest.encode(password);
}
 
public boolean checkPassword(String password)
{
if(password == null)
throw new NullPointerException("Null password");
 
return checkMd5Password(Digest.encode(password));
}
 
public boolean checkMd5Password(String password)
{
return this.password.equals(password);
}
 
/**
*
* @hibernate.many-to-one
*/
public User getBoss()
{
return boss;
}
 
protected void setBoss(User boss)
{
this.boss = boss;
}
 
public void setBoss(User editor, User boss)
throws ModelException
{
if(!editableBy(editor))
throw new ModelSecurityException();
 
this.boss = boss;
}
 
/**
*
* @hibernate.property
*/
public Boolean getSuperuser()
{
return superuser;
}
 
public boolean isSuperuser()
{
return (superuser != null) && superuser.booleanValue();
}
 
protected void setSuperuser(Boolean superuser)
{
this.superuser = superuser;
}
 
public void setSuperuser(User editor, Boolean superuser)
throws ModelException
{
if(!mayChangeSuperuser(editor))
throw new ModelSecurityException();
 
this.superuser = superuser;
}
 
/**
*
* @hibernate.property column="locale"
*/
protected String getLocaleName()
{
return locale.toString();
}
 
protected void setLocaleName(String localeName)
{
String language = null;
String country = null;
 
if(localeName != null) {
StringTokenizer t = new StringTokenizer(localeName, "_");
if(t.hasMoreTokens()) language = t.nextToken();
if(t.hasMoreTokens()) country = t.nextToken();
}
 
if(language == null)
this.locale = Locale.getDefault();
else if(country == null)
this.locale = new Locale(language);
else
this.locale = new Locale(language, country);
}
 
public void setLocaleName(User editor, String localeName)
throws ModelException
{
if(!partEditableBy(editor))
throw new ModelSecurityException();
 
setLocaleName(localeName);
}
 
public Locale getLocale()
{
return locale;
}
 
public void setLocale(User editor, Locale locale)
throws ModelException
{
if(!partEditableBy(editor))
throw new ModelSecurityException();
 
this.locale = locale;
}
 
/**
*
* @hibernate.set lazy="true"
* @hibernate.collection-key column="usr"
* @hibernate.collection-one-to-many class="ak.hostadmiral.core.model.UserLogin"
*/
protected Collection getLoginHistory()
{
return loginHistory;
}
 
public Collection getLogins()
{
return Collections.unmodifiableCollection(loginHistory);
}
 
protected void setLoginHistory(Collection loginHistory)
{
this.loginHistory = loginHistory;
}
 
public boolean equals(Object o)
{
if(o == null || !(o instanceof User)) return false;
 
User u = (User)o;
return (getId() != null) && (u.getId() != null) && (getId().equals(u.getId()));
}
 
protected void update(User origin)
{
this.login = origin.login;
this.boss = origin.boss;
this.superuser = origin.superuser;
this.locale = origin.locale;
}
 
public int hashCode()
{
if(getId() == null)
return 0;
else
return getId().hashCode();
}
 
public String getTypeKey()
{
return ak.hostadmiral.core.CoreResources.TYPE_USER;
}
 
public String getIdentKey()
{
return ak.hostadmiral.core.CoreResources.IDENT_USER;
}
 
public Object[] getIdentParams()
{
return new Object[] { getLogin() };
}
 
public boolean viewableBy(User user)
{
return user.isSuperuser() || user.equals(boss) || user.equals(this);
}
 
public boolean editableBy(User user)
{
return user.isSuperuser() || user.equals(boss);
}
 
public boolean deleteableBy(User user)
{
return !user.equals(this) && (user.isSuperuser() || user.equals(boss));
}
 
// editor is allowed to change some additional properties
public boolean partEditableBy(User user)
{
return user.isSuperuser() || user.equals(boss) || user.equals(this);
}
 
public boolean mayChangeSuperuser(User user)
{
return user.isSuperuser() && !user.equals(this);
}
 
public boolean mayViewAllLogins()
{
return isSuperuser();
}
 
protected static boolean allowedToCreate(UserManager manager, User editor)
throws ModelException
{
return editor.isSuperuser();
}
 
protected static User createLimitedCopy(User origin)
{
User u = new User();
u.setLogin(origin.getLogin());
return u;
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/SystemUser.java
0,0 → 1,134
package ak.hostadmiral.core.model;
 
import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.util.ModelSecurityException;
 
/**
*
* @hibernate.class table="systemusers"
*/
public class SystemUser
extends GeneralModelObject
{
/** user id in the OS */
private Integer uid;
private String name;
private User owner;
 
protected SystemUser()
{
}
 
/**
*
* @hibernate.property
*/
public Integer getUid()
{
return uid;
}
 
protected void setUid(Integer uid)
{
this.uid = uid;
}
 
public void setUid(User editor, Integer uid)
throws ModelException
{
if(!editableBy(editor))
throw new ModelSecurityException();
 
this.uid = uid;
}
 
/**
*
* @hibernate.property
*/
public String getName()
{
return name;
}
 
protected void setName(String name)
{
this.name = name;
}
 
public void setName(User editor, String name)
throws ModelException
{
if(!editableBy(editor))
throw new ModelSecurityException();
 
this.name = name;
}
 
/**
*
* @hibernate.many-to-one
*/
public User getOwner()
{
return owner;
}
 
protected void setOwner(User owner)
{
this.owner = owner;
}
 
public void setOwner(User editor, User owner)
throws ModelException
{
if(!editableBy(editor))
throw new ModelSecurityException();
 
this.owner = owner;
}
 
public String getTypeKey()
{
return ak.hostadmiral.core.CoreResources.TYPE_SYSTEM_USER;
}
 
public String getIdentKey()
{
return ak.hostadmiral.core.CoreResources.IDENT_SYSTEM_USER;
}
 
public Object[] getIdentParams()
{
return new Object[] { getName(), getUid() };
}
 
public boolean viewableBy(User user)
{
return user.isSuperuser() || (owner == null) || user.equals(owner);
}
 
public boolean editableBy(User user)
{
return user.isSuperuser();
}
 
public boolean deleteableBy(User user)
{
return user.isSuperuser();
}
 
protected static boolean allowedToCreate(SystemUserManager manager, User editor)
throws ModelException
{
return editor.isSuperuser();
}
 
protected static SystemUser createLimitedCopy(SystemUser origin)
{
SystemUser u = new SystemUser();
u.setUid(origin.getUid());
u.setName(origin.getName());
return u;
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/ModelObject.java
0,0 → 1,18
package ak.hostadmiral.core.model;
 
public interface ModelObject
{
public Long getId();
 
public String getTypeKey();
 
public String getIdentKey();
 
public Object[] getIdentParams();
 
public boolean viewableBy(User user);
 
public boolean editableBy(User user);
 
public boolean deleteableBy(User user);
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/UserBeforeDeleteListener.java
0,0 → 1,20
package ak.hostadmiral.core.model;
 
import java.util.Collection;
import ak.hostadmiral.util.ModelException;
 
public interface UserBeforeDeleteListener
{
/**
* called if some user is about to be deleted.
*
* @param editor who is doing the operation
* @param user the user to delete
* @param known Collection(Object) - already known objects which are touched by current operation,
* to avoid loops
* @return Collection(CascadeDeleteElement) - object which are touched by deleting the user
* FIXME: limit deep of load?
*/
public Collection userBeforeDelete(User editor, User user, Collection known)
throws ModelException;
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/Mailbox.java
0,0 → 1,231
package ak.hostadmiral.core.model;
 
import ak.hostadmiral.util.Digest;
import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.util.ModelSecurityException;
 
/**
*
* @hibernate.class table="mailboxes"
*/
public class Mailbox
extends GeneralModelObject
{
private String login;
private String password;
private InetDomain domain;
private User owner;
private Boolean virusCheck;
private Boolean spamCheck;
private SystemUser systemUser;
 
protected Mailbox()
{
}
 
/**
*
* @hibernate.property
*/
public String getLogin()
{
return login;
}
 
protected void setLogin(String login)
{
this.login = login;
}
 
public void setLogin(User editor, String login)
throws ModelException
{
if(!editableBy(editor))
throw new ModelSecurityException();
 
this.login = login;
}
 
/**
*
* @hibernate.property
*/
protected String getPassword()
{
return password;
}
 
protected void setPassword(String password)
{
this.password = password;
}
 
public void setPassword(User editor, String password)
throws ModelException
{
if(password == null)
throw new NullPointerException("Null password");
 
this.password = Digest.encode(password);
}
 
/**
*
* @hibernate.many-to-one
*/
public InetDomain getDomain()
{
return domain;
}
 
protected void setDomain(InetDomain domain)
{
this.domain = domain;
}
 
public void setDomain(User editor, InetDomain domain)
throws ModelException
{
if(!editableBy(editor))
throw new ModelSecurityException();
 
this.domain = domain;
}
 
/**
*
* @hibernate.many-to-one
*/
public User getOwner()
{
return owner;
}
 
protected void setOwner(User owner)
{
this.owner = owner;
}
 
public void setOwner(User editor, User owner)
throws ModelException
{
if(!editableBy(editor))
throw new ModelSecurityException();
 
this.owner = owner;
}
 
/**
*
* @hibernate.property
*/
public Boolean getVirusCheck()
{
return virusCheck;
}
 
protected void setVirusCheck(Boolean virusCheck)
{
this.virusCheck = virusCheck;
}
 
public void setVirusCheck(User editor, Boolean virusCheck)
throws ModelException
{
if(!editableBy(editor))
throw new ModelSecurityException();
 
this.virusCheck = virusCheck;
}
 
/**
*
* @hibernate.property
*/
public Boolean getSpamCheck()
{
return spamCheck;
}
 
protected void setSpamCheck(Boolean spamCheck)
{
this.spamCheck = spamCheck;
}
 
public void setSpamCheck(User editor, Boolean spamCheck)
throws ModelException
{
if(!editableBy(editor))
throw new ModelSecurityException();
 
this.spamCheck = spamCheck;
}
 
/**
*
* @hibernate.many-to-one
*/
public SystemUser getSystemUser()
{
return systemUser;
}
 
protected void setSystemUser(SystemUser systemUser)
{
this.systemUser = systemUser;
}
 
public void setSystemUser(User editor, SystemUser systemUser)
throws ModelException
{
if(!editableBy(editor))
throw new ModelSecurityException();
 
this.systemUser = systemUser;
}
 
public String getTypeKey()
{
return ak.hostadmiral.core.CoreResources.TYPE_MAILBOX;
}
 
public String getIdentKey()
{
return ak.hostadmiral.core.CoreResources.IDENT_MAILBOX;
}
 
public Object[] getIdentParams()
{
return new Object[] { getLogin(), getDomain().getName() };
}
 
public boolean viewableBy(User user)
{
return user.isSuperuser() || user.equals(domain.getOwner()) || user.equals(owner);
}
 
public boolean editableBy(User user)
{
return user.isSuperuser() || user.equals(domain.getOwner());
}
 
public boolean deleteableBy(User user)
{
return user.isSuperuser() || user.equals(domain.getOwner());
}
 
protected static boolean allowedToCreate(MailboxManager manager, User editor)
throws ModelException
{
return editor.isSuperuser()
|| InetDomainManager.getInstance().areInetDomainsAvailable(editor);
}
 
protected static Mailbox createLimitedCopy(Mailbox origin)
{
Mailbox u = new Mailbox();
u.setLogin(origin.getLogin());
u.setDomain(origin.getDomain());
return u;
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/SystemUserBeforeDeleteListener.java
0,0 → 1,20
package ak.hostadmiral.core.model;
 
import java.util.Collection;
import ak.hostadmiral.util.ModelException;
 
public interface SystemUserBeforeDeleteListener
{
/**
* called if some system user is about to be deleted.
*
* @param editor who is doing the operation
* @param user the user to delete
* @param known Collection(Object) - already known objects which are touched by current operation,
* to avoid loops
* @return Collection(CascadeDeleteElement) - object which are touched by deleting the user
* FIXME: limit deep of load?
*/
public Collection systemUserBeforeDelete(User editor, SystemUser user, Collection known)
throws ModelException;
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/InetDomainBeforeDeleteListener.java
0,0 → 1,20
package ak.hostadmiral.core.model;
 
import java.util.Collection;
import ak.hostadmiral.util.ModelException;
 
public interface InetDomainBeforeDeleteListener
{
/**
* called if some domain is about to be deleted.
*
* @param editor who is doing the operation
* @param domain the domain to delete
* @param known Collection(Object) - already known objects which are touched by current operation,
* to avoid loops
* @return Collection(CascadeDeleteElement) - object which are touched by deleting the domain
* FIXME: limit deep of load?
*/
public Collection inetDomainBeforeDelete(User editor, InetDomain domain, Collection known)
throws ModelException;
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/UserLogin.java
0,0 → 1,114
package ak.hostadmiral.core.model;
 
import java.util.Date;
 
/**
*
* @hibernate.class table="userlogins"
*/
public class UserLogin
{
private Long id;
private User user;
private String login;
private Date loginTime;
private Boolean success;
private String ip;
 
protected UserLogin()
{
}
 
protected UserLogin(User user, String login, Date loginTime, Boolean success, String ip)
{
this.user = user;
this.login = login;
this.loginTime = loginTime;
this.success = success;
this.ip = ip;
}
 
/**
*
* @hibernate.id generator-class="native"
*/
public Long getId()
{
return id;
}
 
protected void setId(Long id)
{
this.id = id;
}
 
/**
*
* @hibernate.many-to-one column="usr"
*/
public User getUser()
{
return user;
}
 
protected void setUser(User user)
{
this.user = user;
}
 
/**
*
* @hibernate.property
*/
public String getLogin()
{
return login;
}
 
protected void setLogin(String login)
{
this.login = login;
}
 
/**
*
* @hibernate.property
*/
public Date getLoginTime()
{
return loginTime;
}
 
protected void setLoginTime(Date loginTime)
{
this.loginTime = loginTime;
}
 
/**
*
* @hibernate.property
*/
public Boolean getSuccess()
{
return success;
}
 
protected void setSuccess(Boolean success)
{
this.success = success;
}
 
/**
*
* @hibernate.property
*/
public String getIp()
{
return ip;
}
 
protected void setIp(String ip)
{
this.ip = ip;
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/MailboxBeforeDeleteListener.java
0,0 → 1,20
package ak.hostadmiral.core.model;
 
import java.util.Collection;
import ak.hostadmiral.util.ModelException;
 
public interface MailboxBeforeDeleteListener
{
/**
* called if some mailbox is about to be deleted.
*
* @param editor who is doing the operation
* @param mailbox the mailbox to delete
* @param known Collection(Object) - already known objects which are touched by current operation,
* to avoid loops
* @return Collection(CascadeDeleteElement) - object which are touched by deleting the mailbox
* FIXME: limit deep of load?
*/
public Collection mailboxBeforeDelete(User editor, Mailbox mailbox, Collection known)
throws ModelException;
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/MailAliasBeforeDeleteListener.java
0,0 → 1,21
package ak.hostadmiral.core.model;
 
import java.util.Collection;
import ak.hostadmiral.util.ModelException;
 
public interface MailAliasBeforeDeleteListener
{
/**
* called if some mail alias is about to be deleted.
*
* @param editor who is doing the operation
* @param alias the mail alias to delete
* @param known Collection(Object) - already known objects which are touched by current operation,
* to avoid loops
* @return Collection(CascadeDeleteElement)
* - object which are touched by deleting the mail alias
* FIXME: limit deep of load?
*/
public Collection mailAliasBeforeDelete(User editor, MailAlias mailAlias, Collection known)
throws ModelException;
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/CascadeDeleteElement.java
0,0 → 1,55
package ak.hostadmiral.core.model;
 
import java.util.Collection;
 
public class CascadeDeleteElement
{
public static final int FORBIDDEN = 1;
public static final int DELETE = 2;
public static final int CHANGE = 3;
 
private ModelObject object;
private int effect;
private Collection cascade; // Collection(CascadeDeleteElement)
 
public CascadeDeleteElement()
{
}
 
public CascadeDeleteElement(ModelObject object, int effect, Collection cascade)
{
this.object = object;
this.effect = effect;
this.cascade = cascade;
}
 
public ModelObject getObject()
{
return object;
}
 
public void setObject(ModelObject object)
{
this.object = object;
}
 
public int getEffect()
{
return effect;
}
 
public void setEffect(int effect)
{
this.effect = effect;
}
 
public Collection getCascade()
{
return cascade;
}
 
public void setCascade(Collection cascade)
{
this.cascade = cascade;
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/InetDomain.java
0,0 → 1,108
package ak.hostadmiral.core.model;
 
import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.util.ModelSecurityException;
 
/**
*
* @hibernate.class table="domains"
*/
public class InetDomain
extends GeneralModelObject
{
private String name;
private User owner;
 
protected InetDomain()
{
}
 
/**
*
* @hibernate.property
*/
public String getName()
{
return name;
}
 
protected void setName(String name)
{
this.name = name;
}
 
public void setName(User editor, String name)
throws ModelException
{
if(!editableBy(editor))
throw new ModelSecurityException();
 
this.name = name;
}
 
/**
*
* @hibernate.many-to-one
*/
public User getOwner()
{
return owner;
}
 
protected void setOwner(User owner)
{
this.owner = owner;
}
 
public void setOwner(User editor, User owner)
throws ModelException
{
if(!editableBy(editor))
throw new ModelSecurityException();
 
this.owner = owner;
}
 
public String getTypeKey()
{
return ak.hostadmiral.core.CoreResources.TYPE_DOMAIN;
}
 
public String getIdentKey()
{
return ak.hostadmiral.core.CoreResources.IDENT_DOMAIN;
}
 
public Object[] getIdentParams()
{
return new Object[] { getName() };
}
 
public boolean viewableBy(User user)
{
return user.isSuperuser() || user.equals(owner);
}
 
public boolean editableBy(User user)
{
return user.isSuperuser();
}
 
public boolean deleteableBy(User user)
{
return user.isSuperuser();
}
 
protected static boolean allowedToCreate(InetDomainManager manager, User editor)
throws ModelException
{
return editor.isSuperuser();
}
 
protected static InetDomain createLimitedCopy(InetDomain origin)
{
InetDomain d = new InetDomain();
d.setName(origin.getName());
return d;
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/MailAliasDestination.java
0,0 → 1,132
package ak.hostadmiral.core.model;
 
import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.util.ModelSecurityException;
 
/**
*
* @hibernate.class table="mailaliasdests"
*/
public class MailAliasDestination
extends GeneralModelObject
{
private MailAlias alias;
private Mailbox mailbox;
private String email;
 
protected MailAliasDestination()
{
}
 
/**
*
* @hibernate.many-to-one
*/
public MailAlias getAlias()
{
return alias;
}
 
protected void setAlias(MailAlias alias)
{
this.alias = alias;
}
 
public void setAlias(User editor, MailAlias alias)
throws ModelException
{
if(this.alias != null && !editableBy(editor))
throw new ModelSecurityException();
 
this.alias = alias;
}
 
/**
*
* @hibernate.many-to-one
*/
public Mailbox getMailbox()
{
return mailbox;
}
 
protected void setMailbox(Mailbox mailbox)
{
this.mailbox = mailbox;
}
 
public void setMailbox(User editor, Mailbox mailbox)
throws ModelException
{
if(!editableBy(editor))
throw new ModelSecurityException();
 
this.mailbox = mailbox;
}
 
/**
*
* @hibernate.property
*/
public String getEmail()
{
return email;
}
 
protected void setEmail(String email)
{
this.email = email;
}
 
public void setEmail(User editor, String email)
throws ModelException
{
if(!editableBy(editor))
throw new ModelSecurityException();
 
this.email = email;
}
 
public String getTypeKey()
{
return ak.hostadmiral.core.CoreResources.TYPE_MAIL_ALIAS_DESTINATION;
}
 
public String getIdentKey()
{
if(getMailbox() == null)
return ak.hostadmiral.core.CoreResources.IDENT_MAIL_ALIAS_DESTINATION_EXTERNAL;
else
return ak.hostadmiral.core.CoreResources.IDENT_MAIL_ALIAS_DESTINATION_INTERNAL;
}
 
public Object[] getIdentParams()
{
if(getMailbox() == null)
return new Object[] { getEmail() };
else
return new Object[] { getMailbox().getLogin(), getMailbox().getDomain().getName() };
}
 
public boolean viewableBy(User user)
{
return alias.viewableBy(user);
}
 
public boolean editableBy(User user)
{
return alias.mayChangeDestinations(user);
}
 
public boolean deleteableBy(User user)
{
return alias.mayChangeDestinations(user);
}
 
protected static boolean allowedToCreate(MailAliasDestinationManager manager, User editor)
throws ModelException
{
return true;
}
}
 
/hostadmiral/trunk/src/ak/hostadmiral/core/model/GeneralModelObject.java
0,0 → 1,103
package ak.hostadmiral.core.model;
 
import java.util.Date;
import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.util.ModelSecurityException;
 
public abstract class GeneralModelObject
implements ModelObject
{
private Long id;
private Boolean enabled;
private String comment;
private Date modStamp;
private User modUser;
 
/**
*
* @hibernate.id generator-class="native"
*/
public Long getId()
{
return id;
}
 
protected void setId(Long id)
{
this.id = id;
}
 
/**
*
* @hibernate.property
*/
public Boolean getEnabled()
{
return enabled;
}
 
protected void setEnabled(Boolean enabled)
{
this.enabled = enabled;
}
 
public void setEnabled(User editor, Boolean enabled)
throws ModelException
{
if(!editableBy(editor))
throw new ModelSecurityException();
 
this.enabled = enabled;
}
 
/**
*
* @hibernate.property
*/
public String getComment()
{
return comment;
}
 
protected void setComment(String comment)
{
this.comment = comment;
}
 
public void setComment(User editor, String comment)
throws ModelException
{
if(!editableBy(editor))
throw new ModelSecurityException();
 
this.comment = comment;
}
 
/**
*
* @hibernate.timestamp column="mod_stamp"
*/
public Date getModStamp()
{
return modStamp;
}
 
protected void setModStamp(Date modStamp)
{
this.modStamp = modStamp;
}
 
/**
*
* @hibernate.many-to-one column="mod_user"
*/
public User getModUser()
{
return modUser;
}
 
protected void setModUser(User modUser)
{
this.modUser = modUser;
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/MailAlias.java
0,0 → 1,167
package ak.hostadmiral.core.model;
 
import java.util.Collection;
import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.util.ModelSecurityException;
 
/**
*
* @hibernate.class table="mailaliases"
*/
public class MailAlias
extends GeneralModelObject
{
private String address;
private InetDomain domain;
private User owner;
private Collection destinations; // Collection(MailAliasDestintion)
 
protected MailAlias()
{
}
 
/**
*
* @hibernate.property
*/
public String getAddress()
{
return address;
}
 
protected void setAddress(String address)
{
this.address = address;
}
 
public void setAddress(User editor, String address)
throws ModelException
{
if(!editableBy(editor))
throw new ModelSecurityException();
 
this.address = address;
}
 
/**
*
* @hibernate.many-to-one
*/
public InetDomain getDomain()
{
return domain;
}
 
protected void setDomain(InetDomain domain)
{
this.domain = domain;
}
 
public void setDomain(User editor, InetDomain domain)
throws ModelException
{
if(!editableBy(editor))
throw new ModelSecurityException();
 
this.domain = domain;
}
 
/**
*
* @hibernate.many-to-one
*/
public User getOwner()
{
return owner;
}
 
protected void setOwner(User owner)
{
this.owner = owner;
}
 
public void setOwner(User editor, User owner)
throws ModelException
{
if(!editableBy(editor))
throw new ModelSecurityException();
 
this.owner = owner;
}
 
/**
* @return Collection(MailAliasDestination)
*
* @hibernate.bag inverse="true" cascade="all-delete-orphan"
* @hibernate.collection-key column="alias"
* @hibernate.collection-one-to-many class="ak.hostadmiral.core.model.MailAliasDestination"
*/
protected Collection getDestinations()
{
return destinations;
}
 
/**
* @return Collection(MailAliasDestination)
*/
public Collection getDestinations(User editor)
throws ModelException
{
if(mayChangeDestinations(editor))
return destinations;
else if(viewableBy(editor))
return java.util.Collections.unmodifiableCollection(destinations);
else
throw new ModelSecurityException();
}
 
/**
* @param destinations Collection(MailAliasDestination)
*/
protected void setDestinations(Collection destinations)
{
this.destinations = destinations;
}
 
public String getTypeKey()
{
return ak.hostadmiral.core.CoreResources.TYPE_MAIL_ALIAS;
}
 
public String getIdentKey()
{
return ak.hostadmiral.core.CoreResources.IDENT_MAIL_ALIAS;
}
 
public Object[] getIdentParams()
{
return new Object[] { getAddress(), getDomain().getName() };
}
 
public boolean viewableBy(User user)
{
return user.isSuperuser() || user.equals(domain.getOwner()) || user.equals(owner);
}
 
public boolean editableBy(User user)
{
return user.isSuperuser() || user.equals(domain.getOwner());
}
 
public boolean mayChangeDestinations(User user)
{
return user.isSuperuser() || user.equals(domain.getOwner()) || user.equals(owner);
}
 
public boolean deleteableBy(User user)
{
return user.isSuperuser() || user.equals(domain.getOwner());
}
 
protected static boolean allowedToCreate(MailAliasManager manager, User editor)
throws ModelException
{
return editor.isSuperuser()
|| InetDomainManager.getInstance().areInetDomainsAvailable(editor);
}
}