📄 cmsresourcebroker.java
字号:
*
* <B>Security:</B>
* Only users, which are in the group "administrators" are granted.
*
* @param currentUser The user who requested this method.
* @param currentProject The current project of the user.
* @param username The name of the user that is to be added to the group.
* @param groupname The name of the group.
* @throws CmsException Throws CmsException if operation was not succesfull.
*/
public void addUserToGroup(CmsUser currentUser, CmsProject currentProject, String username, String groupname) throws CmsException {
if (!userInGroup(currentUser, currentProject, username, groupname)) {
// Check the security
if (isAdmin(currentUser, currentProject)) {
CmsUser user;
CmsGroup group;
try{
user = readUser(currentUser, currentProject, username);
} catch (CmsException e){
if (e.getType() == CmsException.C_NO_USER){
user = readWebUser(currentUser, currentProject, username);
} else {
throw e;
}
}
//check if the user exists
if (user != null) {
group = readGroup(currentUser, currentProject, groupname);
//check if group exists
if (group != null) {
//add this user to the group
m_dbAccess.addUserToGroup(user.getId(), group.getId());
// update the cache
m_userGroupsCache.clear();
} else {
throw new CmsException("[" + this.getClass().getName() + "]" + groupname, CmsException.C_NO_GROUP);
}
} else {
throw new CmsException("[" + this.getClass().getName() + "]" + username, CmsException.C_NO_USER);
}
} else {
throw new CmsException("[" + this.getClass().getName() + "] " + username, CmsException.C_NO_ACCESS);
}
}
}
/**
* Adds a web user to the Cms. <br>
*
* A web user has no access to the workplace but is able to access personalized
* functions controlled by the OpenCms.
*
* @param currentUser The user who requested this method.
* @param currentProject The current project of the user.
* @param name The new name for the user.
* @param password The new password for the user.
* @param group The default groupname for the user.
* @param description The description for the user.
* @param additionalInfos A Hashtable with additional infos for the user. These
* Infos may be stored into the Usertables (depending on the implementation).
* @param flags The flags for a user (e.g. C_FLAG_ENABLED)
*
* @return user The added user will be returned.
*
* @throws CmsException Throws CmsException if operation was not succesfull.
*/
public CmsUser addWebUser(CmsObject cms, CmsUser currentUser, CmsProject currentProject,
String name, String password,
String group, String description,
Hashtable additionalInfos, int flags)
throws CmsException {
// no space before or after the name
name = name.trim();
// check the username
validFilename(name);
// check the password
Utils.validateNewPassword(cms, password, null);
if( (name.length() > 0) ) {
CmsGroup defaultGroup = readGroup(currentUser, currentProject, group);
CmsUser newUser = m_dbAccess.addUser(name, password, description, " ", " ", " ", 0, 0, C_FLAG_ENABLED, additionalInfos, defaultGroup, " ", " ", C_USER_TYPE_WEBUSER);
CmsUser user;
CmsGroup usergroup;
user=m_dbAccess.readUser(newUser.getName(),C_USER_TYPE_WEBUSER);
//check if the user exists
if (user != null) {
usergroup=readGroup(currentUser,currentProject,group);
//check if group exists
if (usergroup != null){
//add this user to the group
m_dbAccess.addUserToGroup(user.getId(),usergroup.getId());
// update the cache
m_userGroupsCache.clear();
} else {
throw new CmsException("["+this.getClass().getName()+"]"+group,CmsException.C_NO_GROUP);
}
} else {
throw new CmsException("["+this.getClass().getName()+"]"+name,CmsException.C_NO_USER);
}
return newUser;
} else {
throw new CmsException("[" + this.getClass().getName() + "] " + name,
CmsException.C_SHORT_PASSWORD);
}
}
/**
* Adds a web user to the Cms. <br>
*
* A web user has no access to the workplace but is able to access personalized
* functions controlled by the OpenCms.
*
* @param currentUser The user who requested this method.
* @param currentProject The current project of the user.
* @param name The new name for the user.
* @param password The new password for the user.
* @param group The default groupname for the user.
* @param additionalGroup An additional group for the user.
* @param description The description for the user.
* @param additionalInfos A Hashtable with additional infos for the user. These
* Infos may be stored into the Usertables (depending on the implementation).
* @param flags The flags for a user (e.g. C_FLAG_ENABLED)
*
* @return user The added user will be returned.
*
* @throws CmsException Throws CmsException if operation was not succesfull.
*/
public CmsUser addWebUser(CmsObject cms, CmsUser currentUser, CmsProject currentProject,
String name, String password,
String group, String additionalGroup,
String description,
Hashtable additionalInfos, int flags)
throws CmsException {
// no space before or after the name
name = name.trim();
// check the username
validFilename(name);
// check the password
Utils.validateNewPassword(cms, password, null);
if( (name.length() > 0) ) {
CmsGroup defaultGroup = readGroup(currentUser, currentProject, group);
CmsUser newUser = m_dbAccess.addUser(name, password, description, " ", " ", " ", 0, 0, C_FLAG_ENABLED, additionalInfos, defaultGroup, " ", " ", C_USER_TYPE_WEBUSER);
CmsUser user;
CmsGroup usergroup;
CmsGroup addGroup;
user=m_dbAccess.readUser(newUser.getName(),C_USER_TYPE_WEBUSER);
//check if the user exists
if (user != null) {
usergroup=readGroup(currentUser,currentProject,group);
//check if group exists
if (usergroup != null && isWebgroup(usergroup)){
//add this user to the group
m_dbAccess.addUserToGroup(user.getId(),usergroup.getId());
// update the cache
m_userGroupsCache.clear();
} else {
throw new CmsException("["+this.getClass().getName()+"]"+group,CmsException.C_NO_GROUP);
}
// if an additional groupname is given and the group does not belong to
// Users, Administrators or Projectmanager add the user to this group
if (additionalGroup != null && !"".equals(additionalGroup)){
addGroup = readGroup(currentUser, currentProject, additionalGroup);
if(addGroup != null && isWebgroup(addGroup)){
//add this user to the group
m_dbAccess.addUserToGroup(user.getId(), addGroup.getId());
// update the cache
m_userGroupsCache.clear();
} else {
throw new CmsException("["+this.getClass().getName()+"]"+additionalGroup,CmsException.C_NO_GROUP);
}
}
} else {
throw new CmsException("["+this.getClass().getName()+"]"+name,CmsException.C_NO_USER);
}
return newUser;
} else {
throw new CmsException("[" + this.getClass().getName() + "] " + name,
CmsException.C_SHORT_PASSWORD);
}
}
/**
* Returns the anonymous user object.<P/>
*
* <B>Security:</B>
* All users are granted.
*
* @param currentUser The user who requested this method.
* @param currentProject The current project of the user.
* @return the anonymous user object.
* @throws CmsException Throws CmsException if operation was not succesful
*/
public CmsUser anonymousUser(CmsUser currentUser, CmsProject currentProject) throws CmsException
{
return readUser(currentUser, currentProject, C_USER_GUEST);
}
/**
* Changes the group for this resource<br>
*
* Only the group of a resource in an offline project can be changed. The state
* of the resource is set to CHANGED (1).
* If the content of this resource is not exisiting in the offline project already,
* it is read from the online project and written into the offline project.
* The user may change this, if he is admin of the resource. <br>
*
* <B>Security:</B>
* Access is granted, if:
* <ul>
* <li>the user has access to the project</li>
* <li>the user is owner of the resource or is admin</li>
* <li>the resource is locked by the callingUser</li>
* </ul>
*
* @param currentUser The user who requested this method.
* @param currentProject The current project of the user.
* @param filename The complete path to the resource.
* @param newGroup The name of the new group for this resource.
*
* @throws CmsException Throws CmsException if operation was not succesful.
*/
public void chgrp(CmsUser currentUser, CmsProject currentProject,
String filename, String newGroup)
throws CmsException {
CmsResource resource=null;
// read the resource to check the access
if (filename.endsWith("/")) {
resource = readFolder(currentUser,currentProject,filename);
} else {
resource = (CmsFile)readFileHeader(currentUser,currentProject,filename);
}
// has the user write-access? and is he owner or admin?
if( accessWrite(currentUser, currentProject, resource) &&
( (resource.getOwnerId() == currentUser.getId()) ||
isAdmin(currentUser, currentProject))) {
CmsGroup group = readGroup(currentUser, currentProject, newGroup);
resource.setGroupId(group.getId());
// write-acces was granted - write the file.
if (filename.endsWith("/")) {
if (resource.getState()==C_STATE_UNCHANGED) {
resource.setState(C_STATE_CHANGED);
}
m_dbAccess.writeFolder(currentProject,(CmsFolder)resource,true, currentUser.getId());
// update the cache
this.clearResourceCache(filename, currentProject, currentUser);
} else {
m_dbAccess.writeFileHeader(currentProject,(CmsFile)resource,true, currentUser.getId());
if (resource.getState()==C_STATE_UNCHANGED) {
resource.setState(C_STATE_CHANGED);
}
// update the cache
this.clearResourceCache(filename, currentProject, currentUser);
}
// inform about the file-system-change
fileSystemChanged(false);
} else {
throw new CmsException("[" + this.getClass().getName() + "] " + filename,
CmsException.C_NO_ACCESS);
}
}
/**
* Changes the flags for this resource.<br>
*
* Only the flags of a resource in an offline project can be changed. The state
* of the resource is set to CHANGED (1).
* If the content of this resource is not exisiting in the offline project already,
* it is read from the online project and written into the offline project.
* The user may change the flags, if he is admin of the resource <br>.
*
* <B>Security:</B>
* Access is granted, if:
* <ul>
* <li>the user has access to the project</li>
* <li>the user can write the resource</li>
* <li>the resource is locked by the callingUser</li>
* </ul>
*
* @param currentUser The user who requested this method.
* @param currentProject The current project of the user.
* @param filename The complete path to the resource.
* @param flags The new accessflags for the resource.
*
* @throws CmsException Throws CmsException if operation was not succesful.
*/
public void chmod(CmsUser currentUser, CmsProject currentProject,
String filename, int flags)
throws CmsException {
CmsResource resource=null;
// read the resource to check the access
if (filename.endsWith("/")) {
resource = readFolder(currentUser,currentProject,filename);
} else {
resource = (CmsFile)readFileHeader(currentUser,currentProject,filename);
}
// has the user write-access?
if( accessWrite(currentUser, currentProject, resource)||
((resource.isLockedBy() == currentUser.getId() &&
resource.getLockedInProject() == currentProject.getId()) &&
(resource.getOwnerId() == currentUser.getId()||isAdmin(currentUser, currentProject))) ) {
// write-acces was granted - write the file.
//set the flags
resource.setAccessFlags(flags);
//update file
if (filename.endsWith("/")) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -