📄 cmsuserdriver.java
字号:
// otherwise update the already existing entry
stmt = m_sqlManager.getPreparedStatement(conn, project, "C_ACCESS_UPDATE");
stmt.setInt(1, acEntry.getAllowedPermissions());
stmt.setInt(2, acEntry.getDeniedPermissions());
stmt.setInt(3, acEntry.getFlags());
stmt.setString(4, acEntry.getResource().toString());
stmt.setString(5, acEntry.getPrincipal().toString());
stmt.executeUpdate();
} catch (SQLException e) {
throw new CmsDbSqlException(Messages.get().container(
Messages.ERR_GENERIC_SQL_1,
CmsDbSqlException.getErrorQuery(stmt)), e);
} finally {
m_sqlManager.closeAll(dbc, conn, stmt, res);
}
}
/**
* @see org.opencms.db.I_CmsUserDriver#writeGroup(org.opencms.db.CmsDbContext, org.opencms.file.CmsGroup)
*/
public void writeGroup(CmsDbContext dbc, CmsGroup group) throws CmsDataAccessException {
PreparedStatement stmt = null;
Connection conn = null;
if (group != null) {
try {
// create statement
conn = m_sqlManager.getConnection(dbc);
stmt = m_sqlManager.getPreparedStatement(conn, "C_GROUPS_WRITEGROUP");
stmt.setString(1, m_sqlManager.validateEmpty(group.getDescription()));
stmt.setInt(2, group.getFlags());
stmt.setString(3, group.getParentId().toString());
stmt.setString(4, group.getId().toString());
stmt.executeUpdate();
} catch (SQLException e) {
throw new CmsDbSqlException(Messages.get().container(
Messages.ERR_GENERIC_SQL_1,
CmsDbSqlException.getErrorQuery(stmt)), e);
} finally {
m_sqlManager.closeAll(dbc, conn, stmt, null);
}
} else {
throw new CmsDbEntryNotFoundException(org.opencms.db.Messages.get().container(
org.opencms.db.Messages.ERR_UNKNOWN_GROUP_1,
group.getName()));
}
}
/**
* @see org.opencms.db.I_CmsUserDriver#writePassword(org.opencms.db.CmsDbContext, java.lang.String, int, java.lang.String, java.lang.String)
*/
public void writePassword(CmsDbContext dbc, String userName, int type, String oldPassword, String newPassword)
throws CmsDataAccessException, CmsPasswordEncryptionException {
PreparedStatement stmt = null;
Connection conn = null;
// TODO: if old password is not null, check if it is valid
// TODO: use type in user selection
try {
conn = m_sqlManager.getConnection(dbc);
stmt = m_sqlManager.getPreparedStatement(conn, "C_USERS_SETPW");
stmt.setString(1, OpenCms.getPasswordHandler().digest(newPassword));
stmt.setString(2, userName);
stmt.executeUpdate();
} catch (SQLException e) {
throw new CmsDbSqlException(Messages.get().container(
Messages.ERR_GENERIC_SQL_1,
CmsDbSqlException.getErrorQuery(stmt)), e);
} finally {
m_sqlManager.closeAll(dbc, conn, stmt, null);
}
}
/**
* @see org.opencms.db.I_CmsUserDriver#writeUser(org.opencms.db.CmsDbContext, org.opencms.file.CmsUser)
*/
public void writeUser(CmsDbContext dbc, CmsUser user) throws CmsDataAccessException {
PreparedStatement stmt = null;
Connection conn = null;
try {
conn = m_sqlManager.getConnection(dbc);
stmt = m_sqlManager.getPreparedStatement(conn, "C_USERS_WRITE");
// write data to database
stmt.setString(1, m_sqlManager.validateEmpty(user.getDescription()));
stmt.setString(2, m_sqlManager.validateEmpty(user.getFirstname()));
stmt.setString(3, m_sqlManager.validateEmpty(user.getLastname()));
stmt.setString(4, m_sqlManager.validateEmpty(user.getEmail()));
stmt.setLong(5, user.getLastlogin());
stmt.setInt(6, user.getFlags());
m_sqlManager.setBytes(stmt, 7, internalSerializeAdditionalUserInfo(user.getAdditionalInfo()));
stmt.setString(8, m_sqlManager.validateEmpty(user.getAddress()));
stmt.setInt(9, user.getType());
stmt.setString(10, user.getId().toString());
stmt.executeUpdate();
} catch (SQLException e) {
throw new CmsDbSqlException(Messages.get().container(
Messages.ERR_GENERIC_SQL_1,
CmsDbSqlException.getErrorQuery(stmt)), e);
} catch (IOException e) {
throw new CmsDbIoException(
Messages.get().container(Messages.ERR_SERIALIZING_USER_DATA_1, user.getName()),
e);
} finally {
m_sqlManager.closeAll(dbc, conn, stmt, null);
}
}
/**
* @see org.opencms.db.I_CmsUserDriver#writeUserType(org.opencms.db.CmsDbContext, org.opencms.util.CmsUUID, int)
*/
public void writeUserType(CmsDbContext dbc, CmsUUID userId, int userType) throws CmsDataAccessException {
PreparedStatement stmt = null;
Connection conn = null;
try {
conn = m_sqlManager.getConnection(dbc);
stmt = m_sqlManager.getPreparedStatement(conn, "C_USERS_UPDATE_USERTYPE");
// write data to database
stmt.setInt(1, userType);
stmt.setString(2, userId.toString());
stmt.executeUpdate();
} catch (SQLException e) {
throw new CmsDbSqlException(Messages.get().container(
Messages.ERR_GENERIC_SQL_1,
CmsDbSqlException.getErrorQuery(stmt)), e);
} finally {
m_sqlManager.closeAll(dbc, conn, stmt, null);
}
}
/**
* @see java.lang.Object#finalize()
*/
protected void finalize() throws Throwable {
try {
m_sqlManager = null;
m_driverManager = null;
} catch (Throwable t) {
// ignore
}
super.finalize();
}
/**
* Semi-constructor to create a CmsGroup instance from a JDBC result set.
* @param res the JDBC ResultSet
*
* @return CmsGroup the new CmsGroup object
* @throws SQLException in case the result set does not include a requested table attribute
*/
protected CmsGroup internalCreateGroup(ResultSet res) throws SQLException {
return new CmsGroup(
new CmsUUID(res.getString(m_sqlManager.readQuery("C_GROUPS_GROUP_ID"))),
new CmsUUID(res.getString(m_sqlManager.readQuery("C_GROUPS_PARENT_GROUP_ID"))),
res.getString(m_sqlManager.readQuery("C_GROUPS_GROUP_NAME")),
res.getString(m_sqlManager.readQuery("C_GROUPS_GROUP_DESCRIPTION")),
res.getInt(m_sqlManager.readQuery("C_GROUPS_GROUP_FLAGS")));
}
/**
* Semi-constructor to create a CmsUser instance from a JDBC result set.
* @param res the JDBC ResultSet
*
* @return CmsUser the new CmsUser object
* @throws SQLException in case the result set does not include a requested table attribute
* @throws IOException if there is an error in deserializing the user info
* @throws ClassNotFoundException if there is an error in deserializing the user info
*/
protected CmsUser internalCreateUser(ResultSet res) throws SQLException, IOException, ClassNotFoundException {
String userName = res.getString(m_sqlManager.readQuery("C_USERS_USER_NAME"));
// deserialize the additional userinfo hash
ByteArrayInputStream bin = new ByteArrayInputStream(m_sqlManager.getBytes(
res,
m_sqlManager.readQuery("C_USERS_USER_INFO")));
ObjectInputStream oin = new ObjectInputStream(bin);
Map info;
// ensure the user is read even if it's additional infos are defect
try {
info = (Map)oin.readObject();
} catch (IOException e) {
CmsMessageContainer message = Messages.get().container(Messages.ERR_READING_ADDITIONAL_INFO_1, userName);
LOG.error(message.key(), e);
info = new HashMap();
}
return new CmsUser(
new CmsUUID(res.getString(m_sqlManager.readQuery("C_USERS_USER_ID"))),
userName,
res.getString(m_sqlManager.readQuery("C_USERS_USER_PASSWORD")),
res.getString(m_sqlManager.readQuery("C_USERS_USER_DESCRIPTION")),
res.getString(m_sqlManager.readQuery("C_USERS_USER_FIRSTNAME")),
res.getString(m_sqlManager.readQuery("C_USERS_USER_LASTNAME")),
res.getString(m_sqlManager.readQuery("C_USERS_USER_EMAIL")),
res.getLong(m_sqlManager.readQuery("C_USERS_USER_LASTLOGIN")),
res.getInt(m_sqlManager.readQuery("C_USERS_USER_FLAGS")),
info,
res.getString(m_sqlManager.readQuery("C_USERS_USER_ADDRESS")),
res.getInt(m_sqlManager.readQuery("C_USERS_USER_TYPE")));
}
/**
* Serialize additional user information to write it as byte array in the database.<p>
*
* @param additionalUserInfo the HashTable with additional information
* @return byte[] the byte array which is written to the db
* @throws IOException if something goes wrong
*/
protected byte[] internalSerializeAdditionalUserInfo(Map additionalUserInfo) throws IOException {
// serialize the hashtable
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream oout = new ObjectOutputStream(bout);
oout.writeObject(additionalUserInfo != null ? new Hashtable(additionalUserInfo) : null);
oout.close();
return bout.toByteArray();
}
/**
* Initializes the default users and groups.<p>
*
* @param dbc the current database context
*
* @throws CmsDataAccessException if something goes wrong
* @throws CmsPasswordEncryptionException if a password of a default user could not be encrypted
*/
private void fillDefaults(CmsDbContext dbc) throws CmsDataAccessException, CmsPasswordEncryptionException {
String guestGroup = OpenCms.getDefaultUsers().getGroupGuests();
String administratorsGroup = OpenCms.getDefaultUsers().getGroupAdministrators();
String usersGroup = OpenCms.getDefaultUsers().getGroupUsers();
String projectmanagersGroup = OpenCms.getDefaultUsers().getGroupProjectmanagers();
String guestUser = OpenCms.getDefaultUsers().getUserGuest();
String adminUser = OpenCms.getDefaultUsers().getUserAdmin();
String exportUser = OpenCms.getDefaultUsers().getUserExport();
CmsGroup guests, administrators, users, projectmanager;
CmsUser guest, admin, export;
guests = createGroup(
dbc,
CmsUUID.getConstantUUID(guestGroup),
guestGroup,
"The guest group",
I_CmsPrincipal.FLAG_ENABLED,
null,
null);
administrators = createGroup(
dbc,
CmsUUID.getConstantUUID(administratorsGroup),
administratorsGroup,
"The administrators group",
I_CmsPrincipal.FLAG_ENABLED | I_CmsPrincipal.FLAG_GROUP_PROJECT_MANAGER,
null,
null);
users = createGroup(
dbc,
CmsUUID.getConstantUUID(usersGroup),
usersGroup,
"The users group",
I_CmsPrincipal.FLAG_ENABLED
| I_CmsPrincipal.FLAG_GROUP_WORKFLOW_ROLE
| I_CmsPrincipal.FLAG_GROUP_PROJECT_USER,
null,
null);
projectmanager = createGroup(
dbc,
CmsUUID.getConstantUUID(projectmanagersGroup),
projectmanagersGroup,
"The projectmanager group",
I_CmsPrincipal.FLAG_ENABLED
| I_CmsPrincipal.FLAG_GROUP_PROJECT_MANAGER
| I_CmsPrincipal.FLAG_GROUP_PROJECT_USER
| I_CmsPrincipal.FLAG_GROUP_WORKFLOW_ROLE,
users.getName(),
null);
guest = importUser(
dbc,
CmsUUID.getConstantUUID(guestUser),
guestUser,
OpenCms.getPasswordHandler().digest(""),
"The guest user",
" ",
" ",
" ",
0,
I_CmsPrincipal.FLAG_ENABLED,
new Hashtable(),
" ",
CmsUser.USER_TYPE_SYSTEMUSER,
null);
admin = importUser(
dbc,
CmsUUID.getConstantUUID(adminUser),
adminUser,
OpenCms.getPasswordHandler().digest("admin"),
"The admin user",
" ",
" ",
" ",
0,
I_CmsPrincipal.FLAG_ENABLED,
new Hashtable(),
" ",
CmsUser.USER_TYPE_SYSTEMUSER,
null);
createUserInGroup(dbc, guest.getId(), guests.getId(), null);
createUserInGroup(dbc, admin.getId(), administrators.getId(), null);
if (!exportUser.equals(OpenCms.getDefaultUsers().getUserAdmin())
&& !exportUser.equals(OpenCms.getDefaultUsers().getUserGuest())) {
export = importUser(
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -