📄 mrole.java
字号:
/******************************************************************************
* The contents of this file are subject to the Compiere License Version 1.1
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.compiere.org/license.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Original Code is Compiere ERP & CRM Smart Business Solution. The Initial
* Developer of the Original Code is Jorg Janke. Portions created by Jorg Janke
* are Copyright (C) 1999-2005 Jorg Janke.
* All parts are Copyright (C) 1999-2005 ComPiere, Inc. All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.model;
import java.sql.*;
import java.util.*;
import java.util.logging.*;
import org.compiere.util.*;
/**
* Role Model.
* Includes AD_User runtime info for Personal Access
* The class is final, so that you cannot overwrite the security rules.
*
* @author Jorg Janke
* @version $Id: MRole.java,v 1.50 2006/01/09 19:43:48 jjanke Exp $
*/
public final class MRole extends X_AD_Role
{
/**
* Get Default (Client) Role
* @return role
*/
public static MRole getDefault ()
{
if (s_defaultRole == null && Ini.isClient())
return getDefault (Env.getCtx(), false);
return s_defaultRole;
} // getDefault
/**
* Get/Set Default Role.
* @param ctx context
* @param reload if true forces load
* @return role
* @see org.compiere.util.Login#loadPreferences(KeyNamePair, KeyNamePair, java.sql.Timestamp, String)
*/
public static MRole getDefault (Properties ctx, boolean reload)
{
int AD_Role_ID = Env.getContextAsInt(ctx, "#AD_Role_ID");
int AD_User_ID = Env.getContextAsInt(ctx, "#AD_User_ID");
if (!Ini.isClient()) // none for Server
AD_User_ID = 0;
if (reload || s_defaultRole == null)
{
s_defaultRole = get (ctx, AD_Role_ID, AD_User_ID, reload);
}
else if (s_defaultRole.getAD_Role_ID() != AD_Role_ID
|| s_defaultRole.getAD_User_ID() != AD_User_ID)
{
s_defaultRole = get (ctx, AD_Role_ID, AD_User_ID, reload);
}
return s_defaultRole;
} // getDefault
/**
* Get Role for User
* @param ctx context
* @param AD_Role_ID role
* @param AD_User_ID user
* @param reload if true forces load
* @return role
*/
public static MRole get (Properties ctx, int AD_Role_ID, int AD_User_ID, boolean reload)
{
s_log.info("AD_Role_ID=" + AD_Role_ID + ", AD_User_ID=" + AD_User_ID + ", reload=" + reload);
String key = AD_Role_ID + "_" + AD_User_ID;
MRole role = (MRole)s_roles.get (key);
if (role == null || reload)
{
role = new MRole (ctx, AD_Role_ID, null);
s_roles.put (key, role);
if (AD_Role_ID == 0)
{
String trxName = null;
role.load(trxName); // special Handling
}
role.setAD_User_ID(AD_User_ID);
role.loadAccess(reload);
s_log.info(role.toString());
}
return role;
} // get
/**
* Get Role (cached).
* Did not set user - so no access loaded
* @param ctx context
* @param AD_Role_ID role
* @return role
*/
public static MRole get (Properties ctx, int AD_Role_ID)
{
String key = String.valueOf(AD_Role_ID);
MRole role = (MRole)s_roles.get (key);
String trxName = null;
if (role == null)
{
role = new MRole (ctx, AD_Role_ID, trxName);
s_roles.put (key, role);
if (AD_Role_ID == 0) // System Role
{
role.load(trxName); // special Handling
}
}
return role;
} // get
/**
* Get Roles Of Client
* @param ctx context
* @return roles of client
*/
public static MRole[] getOfClient (Properties ctx)
{
String sql = "SELECT * FROM AD_Role WHERE AD_Client_ID=?";
ArrayList<MRole> list = new ArrayList<MRole> ();
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement (sql, null);
pstmt.setInt (1, Env.getAD_Client_ID(ctx));
ResultSet rs = pstmt.executeQuery ();
while (rs.next ())
list.add (new MRole(ctx, rs, null));
rs.close ();
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
s_log.log(Level.SEVERE, sql, e);
}
try
{
if (pstmt != null)
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
}
MRole[] retValue = new MRole[list.size ()];
list.toArray (retValue);
return retValue;
} // getOfClient
/**
* Get Roles With where clause
* @param ctx context
* @param whereClause where clause
* @return roles of client
*/
public static MRole[] getOf (Properties ctx, String whereClause)
{
String sql = "SELECT * FROM AD_Role";
if (whereClause != null && whereClause.length() > 0)
sql += " WHERE " + whereClause;
ArrayList<MRole> list = new ArrayList<MRole> ();
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement (sql, null);
ResultSet rs = pstmt.executeQuery ();
while (rs.next ())
list.add (new MRole(ctx, rs, null));
rs.close ();
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
s_log.log(Level.SEVERE, sql, e);
}
try
{
if (pstmt != null)
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
}
MRole[] retValue = new MRole[list.size ()];
list.toArray (retValue);
return retValue;
} // getOf
/** Default Role */
private static MRole s_defaultRole = null;
/** Role/User Cache */
private static CCache<String,MRole> s_roles = new CCache<String,MRole>("AD_Role", 5);
/** Log */
private static CLogger s_log = CLogger.getCLogger(MRole.class);
/** Access SQL Read Write */
public static final boolean SQL_RW = true;
/** Access SQL Read Only */
public static final boolean SQL_RO = false;
/** Access SQL Fully Qualified */
public static final boolean SQL_FULLYQUALIFIED = true;
/** Access SQL Not Fully Qualified */
public static final boolean SQL_NOTQUALIFIED = false;
/** The AD_User_ID of the SuperUser */
public static final int SUPERUSER_USER_ID = 100;
/** The AD_User_ID of the System Administrator */
public static final int SYSTEM_USER_ID = 0;
/**************************************************************************
* Standard Constructor
* @param ctx context
* @param AD_Role_ID id
*/
public MRole (Properties ctx, int AD_Role_ID, String trxName)
{
super (ctx, AD_Role_ID, trxName);
// ID=0 == System Administrator
if (AD_Role_ID == 0)
{
// setName (null);
setIsCanExport (true);
setIsCanReport (true);
setIsManual (false);
setIsPersonalAccess (false);
setIsPersonalLock (false);
setIsShowAcct (false);
setIsAccessAllOrgs(false);
setUserLevel (USERLEVEL_Organization);
setPreferenceType(PREFERENCETYPE_Organization);
setIsChangeLog(false);
setOverwritePriceLimit(false);
setIsUseUserOrgAccess(false);
setMaxQueryRecords(0);
setConfirmQueryRecords(0);
}
} // MRole
/**
* Load Constructor
* @param ctx context
* @param rs result set
*/
public MRole(Properties ctx, ResultSet rs, String trxName)
{
super(ctx, rs, trxName);
} // MRole
/**
* Get Confirm Query Records
* @return entered records or 500 (default)
*/
public int getConfirmQueryRecords ()
{
int no = super.getConfirmQueryRecords ();
if (no == 0)
return 500;
return no;
} // getConfirmQueryRecords
/**
* Require Query
* @param noRecords records
* @return true if query required
*/
public boolean isQueryRequire (int noRecords)
{
if (noRecords < 2)
return false;
int max = getMaxQueryRecords();
if (max > 0 && noRecords > max)
return true;
int qu = getConfirmQueryRecords();
return (noRecords > qu);
} // isQueryRequire
/**
* Over max Query
* @param noRecords records
* @return true if over max query
*/
public boolean isQueryMax (int noRecords)
{
int max = getMaxQueryRecords();
return max > 0 && noRecords > max;
} // isQueryMax
/**
* After Save
* @param newRecord new
* @param success success
* @return success
*/
protected boolean afterSave (boolean newRecord, boolean success)
{
if (newRecord && success)
{
// Add Role to SuperUser
MUserRoles su = new MUserRoles(getCtx(), SUPERUSER_USER_ID, getAD_Role_ID(), get_TrxName());
su.save();
// Add Role to User
if (getCreatedBy() != SUPERUSER_USER_ID)
{
MUserRoles ur = new MUserRoles(getCtx(), getCreatedBy(), getAD_Role_ID(), get_TrxName());
ur.save();
}
updateAccessRecords();
}
//
else if (is_ValueChanged("UserLevel"))
updateAccessRecords();
// Default Role changed
if (s_defaultRole != null
&& s_defaultRole.get_ID() == get_ID())
s_defaultRole = this;
return success;
} // afterSave
/**
* Create Access Records
* @return info
*/
public String updateAccessRecords ()
{
if (isManual())
return "-";
String roleClientOrgUser = getAD_Role_ID() + ","
+ getAD_Client_ID() + "," + getAD_Org_ID() + ",'Y', SysDate,"
+ getUpdatedBy() + ", SysDate," + getUpdatedBy()
+ ",'Y' "; // IsReadWrite
String sqlWindow = "INSERT INTO AD_Window_Access "
+ "(AD_Window_ID, AD_Role_ID,"
+ " AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,IsReadWrite) "
+ "SELECT DISTINCT w.AD_Window_ID, " + roleClientOrgUser
+ "FROM AD_Window w"
+ " INNER JOIN AD_Tab t ON (w.AD_Window_ID=t.AD_Window_ID)"
+ " INNER JOIN AD_Table tt ON (t.AD_Table_ID=tt.AD_Table_ID) "
+ "WHERE t.SeqNo=(SELECT MIN(SeqNo) FROM AD_Tab xt " // only check first tab
+ "WHERE xt.AD_Window_ID=w.AD_Window_ID)"
+ "AND tt.AccessLevel IN ";
String sqlProcess = "INSERT INTO AD_Process_Access "
+ "(AD_Process_ID, AD_Role_ID,"
+ " AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,IsReadWrite) "
+ "SELECT DISTINCT p.AD_Process_ID, " + roleClientOrgUser
+ "FROM AD_Process p "
+ "WHERE AccessLevel IN ";
String sqlForm = "INSERT INTO AD_Form_Access "
+ "(AD_Form_ID, AD_Role_ID,"
+ " AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,IsReadWrite) "
+ "SELECT f.AD_Form_ID, " + roleClientOrgUser
+ "FROM AD_Form f "
+ "WHERE AccessLevel IN ";
String sqlWorkflow = "INSERT INTO AD_WorkFlow_Access "
+ "(AD_WorkFlow_ID, AD_Role_ID,"
+ " AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,IsReadWrite) "
+ "SELECT w.AD_WorkFlow_ID, " + roleClientOrgUser
+ "FROM AD_WorkFlow w "
+ "WHERE AccessLevel IN ";
/**
* Fill AD_xx_Access
* ---------------------------------------------------------------------------
* SCO# Levels S__ 100 4 System info
* SCO 111 7 System shared info
* SC_ 110 6 System/Client info
* _CO 011 3 Client shared info
* _C_ 011 2 Client
* __O 001 1 Organization info
* Roles:
* S 4,7,6
* _CO 7,6,3,2,1
* __O 3,1,7
*/
String accessLevel = null;
if (USERLEVEL_System.equals(getUserLevel()))
accessLevel = "('4','7','6')";
else if (USERLEVEL_Client.equals(getUserLevel()))
accessLevel = "('7','6','3','2')";
else if (USERLEVEL_ClientPlusOrganization.equals(getUserLevel()))
accessLevel = "('7','6','3','2','1')";
else // if (USERLEVEL_Organization.equals(getUserLevel()))
accessLevel = "('3','1','7')";
//
String whereDel = " WHERE AD_Role_ID=" + getAD_Role_ID();
//
int winDel = DB.executeUpdate("DELETE AD_Window_Access" + whereDel, get_TrxName());
int win = DB.executeUpdate(sqlWindow + accessLevel, get_TrxName());
int procDel = DB.executeUpdate("DELETE AD_Process_Access" + whereDel, get_TrxName());
int proc = DB.executeUpdate(sqlProcess + accessLevel, get_TrxName());
int formDel = DB.executeUpdate("DELETE AD_Form_Access" + whereDel, get_TrxName());
int form = DB.executeUpdate(sqlForm + accessLevel, get_TrxName());
int wfDel = DB.executeUpdate("DELETE AD_WorkFlow_Access" + whereDel, get_TrxName());
int wf = DB.executeUpdate(sqlWorkflow + accessLevel, get_TrxName());
log.fine("AD_Window_ID=" + winDel + "+" + win
+ ", AD_Process_ID=" + procDel + "+" + proc
+ ", AD_Form_ID=" + formDel + "+" + form
+ ", AD_Workflow_ID=" + wfDel + "+" + wf);
loadAccess(true);
return "@AD_Window_ID@ #" + win
+ " - @AD_Process_ID@ #" + proc
+ " - @AD_Form_ID@ #" + form
+ " - @AD_Workflow_ID@ #" + wf;
} // createAccessRecords
/**
* String Representation
* @return info
*/
public String toString()
{
StringBuffer sb = new StringBuffer("MRole[");
sb.append(getAD_Role_ID()).append(",").append(getName())
.append(",UserLevel=").append(getUserLevel())
.append(",").append(getClientWhere(false))
.append(",").append(getOrgWhere(false))
.append("]");
return sb.toString();
} // toString
/**
* Extended String Representation
* @param ctx Properties
* @return extended info
*/
public String toStringX (Properties ctx)
{
StringBuffer sb = new StringBuffer();
sb.append(Msg.translate(ctx, "AD_Role_ID")).append("=").append(getName())
.append(" - ").append(Msg.translate(ctx, "IsCanExport")).append("=").append(isCanExport())
.append(" - ").append(Msg.translate(ctx, "IsCanReport")).append("=").append(isCanReport())
.append(Env.NL).append(Env.NL);
//
for (int i = 0; i < m_orgAccess.length; i++)
sb.append(m_orgAccess[i].toString()).append(Env.NL);
sb.append(Env.NL);
//
loadTableAccess(false);
for (int i = 0; i < m_tableAccess.length; i++)
sb.append(m_tableAccess[i].toStringX(ctx)).append(Env.NL);
if (m_tableAccess.length > 0)
sb.append(Env.NL);
//
loadColumnAccess(false);
for (int i = 0; i < m_columnAccess.length; i++)
sb.append(m_columnAccess[i].toStringX(ctx)).append(Env.NL);
if (m_columnAccess.length > 0)
sb.append(Env.NL);
//
loadRecordAccess(false);
for (int i = 0; i < m_recordAccess.length; i++)
sb.append(m_recordAccess[i].toStringX(ctx)).append(Env.NL);
return sb.toString();
} // toStringX
/*************************************************************************
* Access Management
************************************************************************/
/** User */
private int m_AD_User_ID = -1;
/** Positive List of Organizational Access */
private OrgAccess[] m_orgAccess = null;
/** List of Table Access */
private MTableAccess[] m_tableAccess = null;
/** List of Column Access */
private MColumnAccess[] m_columnAccess = null;
/** List of Record Access */
private MRecordAccess[] m_recordAccess = null;
/** List of Dependent Record Access */
private MRecordAccess[] m_recordDependentAccess = null;
/** Table Data Access Level */
private HashMap<Integer,String> m_tableAccessLevel = null;
/** Table Name */
private HashMap<String,Integer> m_tableName = null;
/** Window Access */
private HashMap<Integer,Boolean> m_windowAccess = null;
/** Process Access */
private HashMap<Integer,Boolean> m_processAccess = null;
/** Task Access */
private HashMap<Integer,Boolean> m_taskAccess = null;
/** Workflow Access */
private HashMap<Integer,Boolean> m_workflowAccess = null;
/** Form Access */
private HashMap<Integer,Boolean> m_formAccess = null;
/**
* Set Logged in user
* @param AD_User_ID user requesting info
*/
public void setAD_User_ID(int AD_User_ID)
{
m_AD_User_ID = AD_User_ID;
} // setAD_User_ID
/**
* Get Logged in user
* @return AD_User_ID user requesting info
*/
public int getAD_User_ID()
{
return m_AD_User_ID;
} // getAD_User_ID
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -