📄 muser.java
字号:
return sb.toString ();
} // toString
/**
* Is it an Online Access User
* @return true if it has an email and password
*/
public boolean isOnline ()
{
if (getEMail() == null || getPassword() == null)
return false;
return true;
} // isOnline
/**
* Convert EMail
* @return Valid Internet Address
*/
public InternetAddress getInternetAddress ()
{
String email = getEMail();
if (email == null || email.length() == 0)
return null;
try
{
InternetAddress ia = new InternetAddress (email, true);
if (ia != null)
ia.validate(); // throws AddressException
return ia;
}
catch (AddressException ex)
{
log.warning(email + " - " + ex.getLocalizedMessage());
}
return null;
} // getInternetAddress
/**
* Validate Email (does not work).
* Check DNS MX record
* @param ia email
* @return error message or ""
*/
private String validateEmail (InternetAddress ia)
{
if (ia == null)
return "NoEmail";
if (true)
return null;
Hashtable<String,String> env = new Hashtable<String,String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
// env.put(Context.PROVIDER_URL, "dns://admin.compiere.org");
try
{
DirContext ctx = new InitialDirContext(env);
// Attributes atts = ctx.getAttributes("admin");
Attributes atts = ctx.getAttributes("dns://admin.compiere.org", new String[] {"MX"});
NamingEnumeration en = atts.getAll();
// NamingEnumeration en = ctx.list("compiere.org");
while (en.hasMore())
{
System.out.println(en.next());
}
/**/
}
catch (Exception e)
{
e.printStackTrace();
return e.getLocalizedMessage();
}
return null;
} // validateEmail
/**
* Is the email valid
* @return return true if email is valid (artificial check)
*/
public boolean isEMailValid()
{
return validateEmail(getInternetAddress()) == null;
} // isEMailValid
/**
* Could we send an email
* @return true if EMail Uwer/PW exists
*/
public boolean isCanSendEMail()
{
String s = getEMailUser();
if (s == null || s.length() == 0)
return false;
s = getEMailUserPW();
return s != null && s.length() > 0;
} // isCanSendEMail
/**
* Get EMail Validation Code
* @return code
*/
public String getEMailVerifyCode()
{
long code = getAD_User_ID()
+ getName().hashCode();
return "C" + String.valueOf(Math.abs(code)) + "C";
} // getEMailValidationCode
/**
* Check & Set EMail Validation Code.
* @param code code
* @param info info
* @return true if valid
*/
public boolean setEMailVerifyCode (String code, String info)
{
boolean ok = code != null
&& code.equals(getEMailVerifyCode());
if (ok)
setEMailVerifyDate(new Timestamp(System.currentTimeMillis()));
else
setEMailVerifyDate(null);
setEMailVerify(info);
return ok;
} // setEMailValidationCode
/**
* Is EMail Verified by response
* @return true if verified
*/
public boolean isEMailVerified()
{
// UPDATE AD_User SET EMailVerifyDate=SysDate, EMailVerify='Direct' WHERE AD_User_ID=1
return getEMailVerifyDate() != null
&& getEMailVerify() != null
&& getEMailVerify().length() > 0;
} // isEMailVerified
/**
* Get Notification via EMail
* @return true if email
*/
public boolean isNotificationEMail()
{
String s = getNotificationType();
return s == null || NOTIFICATIONTYPE_EMail.equals(s);
} // isNotificationEMail
/**
* Get Notification via Note
* @return true if note
*/
public boolean isNotificationNote()
{
String s = getNotificationType();
return s != null && NOTIFICATIONTYPE_Notice.equals(s);
} // isNotificationNote
/**************************************************************************
* Get User Roles for Org
* @param AD_Org_ID org
* @return array of roles
*/
public MRole[] getRoles (int AD_Org_ID)
{
if (m_roles != null && m_rolesAD_Org_ID == AD_Org_ID)
return m_roles;
ArrayList<MRole> list = new ArrayList<MRole>();
String sql = "SELECT * FROM AD_Role r "
+ "WHERE r.IsActive='Y'"
+ " AND EXISTS (SELECT * FROM AD_Role_OrgAccess ro"
+ " WHERE r.AD_Role_ID=ro.AD_Role_ID AND ro.IsActive='Y' AND ro.AD_Org_ID=?)"
+ " AND EXISTS (SELECT * FROM AD_User_Roles ur"
+ " WHERE r.AD_Role_ID=ur.AD_Role_ID AND ur.IsActive='Y' AND ur.AD_User_ID=?) "
+ "ORDER BY AD_Role_ID";
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement (sql, get_TrxName());
pstmt.setInt (1, AD_Org_ID);
pstmt.setInt (2, getAD_User_ID());
ResultSet rs = pstmt.executeQuery ();
while (rs.next ())
list.add (new MRole(getCtx(), rs, get_TrxName()));
rs.close ();
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
log.log(Level.SEVERE, sql, e);
}
try
{
if (pstmt != null)
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
}
//
m_rolesAD_Org_ID = AD_Org_ID;
m_roles = new MRole[list.size()];
list.toArray (m_roles);
return m_roles;
} // getRoles
/**
* Is User an Administrator?
* @return true id Admin
*/
public boolean isAdministrator()
{
if (m_isAdministrator == null)
{
m_isAdministrator = Boolean.FALSE;
MRole[] roles = getRoles(0);
for (int i = 0; i < roles.length; i++)
{
if (roles[i].getAD_Role_ID() == 0)
{
m_isAdministrator = Boolean.TRUE;
break;
}
}
}
return m_isAdministrator.booleanValue();
} // isAdministrator
/**
* Has the user Access to BP info and resources
* @param BPAccessType access type
* @param params opt parameter
* @return true if access
*/
public boolean hasBPAccess (String BPAccessType, Object[] params)
{
if (isFullBPAccess())
return true;
getBPAccess(false);
for (int i = 0; i < m_bpAccess.length; i++)
{
if (m_bpAccess[i].getBPAccessType().equals(BPAccessType))
{
return true;
}
}
return false;
} // hasBPAccess
/**
* Get active BP Access records
* @param requery requery
* @return access list
*/
public X_AD_UserBPAccess[] getBPAccess (boolean requery)
{
if (m_bpAccess != null && !requery)
return m_bpAccess;
String sql = "SELECT * FROM AD_UserBPAccess WHERE AD_User_ID=? AND IsActive='Y'";
ArrayList<X_AD_UserBPAccess> list = new ArrayList<X_AD_UserBPAccess>();
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement (sql, null);
pstmt.setInt (1, getAD_User_ID());
ResultSet rs = pstmt.executeQuery ();
while (rs.next ())
{
list.add (new X_AD_UserBPAccess (getCtx(), rs, null));
}
rs.close ();
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
log.log (Level.SEVERE, sql, e);
}
try
{
if (pstmt != null)
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
}
m_bpAccess = new X_AD_UserBPAccess[list.size ()];
list.toArray (m_bpAccess);
return m_bpAccess;
} // getBPAccess
/**
* Before Save
* @param newRecord new
* @return true
*/
protected boolean beforeSave (boolean newRecord)
{
// New Address invalidates verification
if (!newRecord && is_ValueChanged("EMail"))
setEMailVerifyDate(null);
return true;
} // beforeSave
/**
* Test
* @param args ignored
*
public static void main (String[] args)
{
try
{
validateEmail(new InternetAddress("jjanke@compiere.org"));
}
catch (Exception e)
{
e.printStackTrace();
}
// org.compiere.Compiere.startupClient();
// System.out.println ( MUser.get(Env.getCtx(), "SuperUser", "22") );
} // main /* */
} // MUser
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -