📄 profilemanagerproxy.java
字号:
package com.gs.db;
import java.util.*;
//JDK1.1// import com.sun.java.util.collections.*;
/**
* Protection proxy for the ProfileManager class. It restricts access to
* protected methods by throwing UnauthorizedExceptions when necessary.
*
* @see ProfileManager
*/
public class ProfileManagerProxy implements ProfileManager {
private ProfileManager profileManager;
private Authorization authorization;
private IofficePermissions permissions;
/**
* Creates a new ProfileManagerProxy.
*/
public ProfileManagerProxy(ProfileManager profileManager, Authorization
authorization, IofficePermissions permissions)
{
this.profileManager = profileManager;
this.authorization = authorization;
this.permissions = permissions;
}
/**
* Factory method for creating a new User. A password, email address, and
* unique username are all required fields to create a new User.
*
* @param username the new and unique username for the account.
* @param password the password for the account as plain text.
* @param email the email address for the account.
* @return a new User.
* @throws UserAlreadyExistsException if the username already exists in
* the system.
*/
public User createUser(String username, String password, String email)
throws UserAlreadyExistsException
{
return profileManager.createUser(username, password, email);
}
/**
* Factory method for creating a new Group associated with a certain unit.
*
* @param name the new and unique name for the group.
* @param unit the unit to asscociate with
* @param priority the priority level assigned to the group
* @return a new Group.
* @throws GroupAlreadyExistsException if the group name already exists in
* the system.
*/
public Group createGroup(String name) throws UnauthorizedException,
GroupAlreadyExistsException
{
if (permissions.get(IofficePermissions.SYSTEM_ADMIN)) {
Group group = profileManager.createGroup(name);
return new GroupProxy(group, authorization, permissions);
}
else {
throw new UnauthorizedException();
}
}
/**
* Factory method for creating a new Group associated with a certain unit.
*
* @param name the new and unique name for the group.
* @param unit the unit to asscociate with
* @param priority the priority level assigned to the group
* @return a new Group.
* @throws GroupAlreadyExistsException if the group name already exists in
* the system.
*/
public Group createGroup(String name,Unit unit,int priority) throws UnauthorizedException,
GroupAlreadyExistsException {
if (permissions.get(IofficePermissions.SYSTEM_ADMIN)) {
Group group = profileManager.createGroup(name, unit, priority);
return new GroupProxy(group, authorization, permissions);
}
else {
throw new UnauthorizedException();
}
}
/**
* Returns a User specified by their id.
*
* @param userid the id of the User to lookup.
* @return the User specified by the given id.
* @throws UserNotFoundException if the user does not exist.
*/
public User getUser(int userID) throws UserNotFoundException {
User user = profileManager.getUser(userID);
IofficePermissions userPermissions = user.getPermissions(authorization);
IofficePermissions newPermissions =
new IofficePermissions(permissions, userPermissions);
return new UserProxy(user, authorization, newPermissions);
}
/**
* Returns a User specified by username.
*
* throws UserNotFoundException if the user does not exist.
*/
public User getUser(String username) throws UserNotFoundException {
User user = profileManager.getUser(username);
IofficePermissions userPermissions = user.getPermissions(authorization);
IofficePermissions newPermissions =
new IofficePermissions(permissions, userPermissions);
return new UserProxy(user, authorization, newPermissions);
}
/**
* Returns the special "anonymous user" object.
*/
public User getAnonymousUser() {
return profileManager.getAnonymousUser();
}
/**
* Returns the "special user" object. The special user represents any
* valid user in the system. Getting a handle on this object is only
* really useful for setting permissions. For example, if you
* want to allow any registered user to post news, add
* a user permission for posting messages with the special user as the
* User parameter.
*/
public User getSpecialUser() {
return profileManager.getSpecialUser();
}
/**
* Gets a Group by ID.
*
* throws GroupNotFoundException if the group does not exist.
*/
public Group getGroup(int groupID) throws GroupNotFoundException {
Group group = profileManager.getGroup(groupID);
IofficePermissions groupPermissions = group.getPermissions(authorization);
IofficePermissions newPermissions =
new IofficePermissions(permissions, groupPermissions);
return new GroupProxy(group, authorization, newPermissions);
}
/**
* Gets a Group by name.
*
* throws GroupNotFoundException if the group does not exist.
*/
public Group getGroup(String name) throws GroupNotFoundException {
Group group = profileManager.getGroup(name);
IofficePermissions groupPermissions = group.getPermissions(authorization);
IofficePermissions newPermissions =
new IofficePermissions(permissions, groupPermissions);
return new GroupProxy(group, authorization, newPermissions);
}
/**
* Deletes a User.
*
* @param user the user to delete.
* @throws UnauthorizedException
*/
public void deleteUser(User user) throws UnauthorizedException {
if (permissions.get(IofficePermissions.SYSTEM_ADMIN)) {
profileManager.deleteUser(user);
}
else {
throw new UnauthorizedException();
}
}
/**
* Deletes a Group.
*
* @param group the group to delete.
* @throws UnauthorizedException
*/
public void deleteGroup(Group group) throws UnauthorizedException {
if (permissions.get(IofficePermissions.SYSTEM_ADMIN)) {
profileManager.deleteGroup(group);
}
else {
throw new UnauthorizedException();
}
}
/**
* Returns the numer of users in the system.
*/
public int getUserCount() {
return profileManager.getUserCount();
}
/**
* Returns the number of groups in the system.
*/
public int getGroupCount() {
return profileManager.getGroupCount();
}
/**
* Retruns an iterator for all users.
*/
public Iterator users() {
Iterator iterator = profileManager.users();
return new UserIteratorProxy(iterator, authorization, permissions);
}
/**
* Returns an iterator for all users starting at startIndex with the
* given number of results. This is useful to support pagination in a GUI
* where you may only want to display a certain number of results per page.
* It is possible that the number of results returned will be less than
* that specified by numResults if numResults is greater than the number
* of records left in the system to display.
*
* @param startIndex the beginning index to start the results at.
* @param numResults the total number of results to return.
* @return an Iterator for all users in the specified range.
*/
public Iterator users(int startIndex, int numResults) {
Iterator iterator = profileManager.users(startIndex, numResults);
return new UserIteratorProxy(iterator, authorization, permissions);
}
/**
* Returns an iterator for all groups in the system.
*
* @return an Iterator for all groups.
*/
public Iterator groups() {
Iterator iterator = profileManager.groups();
return new GroupIteratorProxy(iterator, authorization, permissions);
}
/**
* Returns an iterator for all groups starting at startIndex with the
* given number of results. This is useful to support pagination in a GUI
* where you may only want to display a certain number of results per page.
* It is possible that the number of results returned will be less than
* that specified by numResults if numResults is greater than the number
* of records left in the system to display.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -