ovirt筆記:經過登陸策略配置模塊來熟悉系統的框架

最近在寫登陸策略配置LoginConfig模塊,如今經過分析登陸策略配置模塊來講明系統的層次結構。系統後臺主要包括四層:java

1. 數據庫層:包括表、存儲過程、實體類
2. 數據訪問層:數據訪問對象(也就是Dao層)
3. 後端邏輯層:查詢與命令類
4. Rest接口層:資源類

數據庫層

登陸限制策略模塊包括一張login_configs表,6個存儲過程(Insertloginconfigs,Updateloginconfigs,Deleteloginconfigs,GetAllFromloginconfigs,GetloginconfigsById和GetloginconfigsByName),以及一個LoginConfis的實體類
LoginConfigs表格只有6個字段,在create_tables.sql中建立表格sql

//create_tables.sql
CREATE TABLE login_configs (
    loginconfig_id uuid NOT NULL,
    role_name character varying(255) NOT NULL,
    time_limit character varying(4000),
    ip_limit character varying(4000),
    mac_limit character varying(4000),
    other character varying(4000)
);

存儲過程:好比增長Insertloginconfigs數據庫

CREATE OR REPLACE FUNCTION Insertloginconfigs(
    v_loginconfig_id uuid,
    v_role_name VARCHAR(255),
    v_time_limit VARCHAR(4000),
    v_ip_limit VARCHAR(4000),
    v_mac_limit VARCHAR(4000),
    v_other VARCHAR(4000)
  )RETURNS VOID AS $PROCEDURE$
BEGIN
    INSERT INTO login_configs (loginconfig_id,role_name,time_limit,ip_limit,mac_limit,other)
    VALUES (v_loginconfig_id,v_role_name,pgp_sym_encrypt(v_time_limit,'engine'),pgp_sym_encrypt(v_ip_limit,'engine'),pgp_sym_encrypt(v_mac_limit,'engine'),pgp_sym_encrypt(v_other,'engine'));
END;$PROCEDURE$
LANGUAGE plpgsql;

實體類LoginConfig後端

public class LoginConfigs implements IVdcQueryable, BusinessEntity<Guid>{
    private static final long serialVersionUID = 3090608330047174149L;
    //進程Id
    private Guid id;
    @Override
    public Guid getId() {
        return id;
    }
    @Override
    public void setId(Guid id) {
        this.id = id;
    }
    //角色名
    private String roleName;
    public String getRoleName() {
        return roleName;
    }
    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }
    //限制時間
    private String timeLimit;
    public String getTimeLimit() {
        return timeLimit;
    }
    public void setTimeLimit(String timeLimit) {
        this.timeLimit = timeLimit;
    }
    //限制IP
    private String macLimit;
    public String getMacLimit() {
        return macLimit;
    }
    public void setMacLimit(String macLimit) {
        this.macLimit = macLimit;
    }
    //限制IP
    private String ipLimit;
    public String getIpLimit() {
        return ipLimit;
    }
    public void setIpLimit(String ipLimit) {
        this.ipLimit = ipLimit;
    }
    //預留字段
    private String other;
    public String getOther() {
        return other;
    }
    public void setOther(String other) {
        this.other = other;
    }
    public LoginConfigs(){
        id = Guid.Empty;
        roleName = "";
        timeLimit = "";
        ipLimit = "";
        other = "";
    }
    @Override
    public Object getQueryableId() {
        return getId();
    }
    @Override
    public int hashCode() {
        return Objects.hash(id,roleName,timeLimit,ipLimit,other);
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof LoginConfigs)) {
            return false;
        }
        LoginConfigs configs = (LoginConfigs) obj;
        return Objects.equals(id, configs.id)
                && Objects.equals(roleName, configs.roleName)
                && Objects.equals(timeLimit, configs.timeLimit)
                && Objects.equals(ipLimit, configs.ipLimit)
                && Objects.equals(other, configs.other);
    }
}

添加完LoginConfigs.java後配置在Common.gwt.xml中,不然 將不能被部分包所使用session

數據訪問層

DbFacade:數據庫的外觀器
LoginConfigsDao:數據訪問對象接口層app

//LoginConfigsDao.java
public interface LoginConfigsDao extends Dao,SearchDao<LoginConfigs>{
    void save(LoginConfigs loginConfigs);
    void update(LoginConfigs loginConfigs);
    void detele(Guid id);
    LoginConfigs getById(Guid id);
    LoginConfigs getByName(String roleName);
    List<LoginConfigs> getAll();
}

LoginConfigsDaoImpl:數據訪問對象實現類ide

public class LoginConfigsDaoImpl extends BaseDao implements LoginConfigsDao{
    private RowMapper<LoginConfigs> loginConfigsRowMapper = (rs ,rowNum) -> {
        LoginConfigs entity = new LoginConfigs();
        entity.setId(getGuidDefaultEmpty(rs,"loginconfig_id"));
        entity.setRoleName(rs.getString("role_name"));
        entity.setTimeLimit(rs.getString("time_limit"));
        entity.setIpLimit(rs.getString("ip_limit"));
        entity.setMacLimit(rs.getString("mac_limit"));
        entity.setOther(rs.getString("other"));
        return entity;
    };
    @Override
    public List<LoginConfigs> getAllWithQuery(String query) {
        return null;
    }
    @Override
    public void save(LoginConfigs loginConfigs) {
        Guid id = loginConfigs.getId();
        if(Guid.isNullOrEmpty(id)){
            id = Guid.newGuid();
            loginConfigs.setId(id);
        }
        getCallsHandler().executeModification("Insertloginconfigs",createFullParametersMapper(loginConfigs ));
    }
    @Override
    public void update(LoginConfigs loginConfigs) {
        getCallsHandler().executeModification("Updateloginconfigs", createFullParametersMapper(loginConfigs));
    }
    @Override
    public void detele(Guid id) {
        getCallsHandler().executeModification("Deleteloginconfigs", createIdParameterMapper(id));
    }
    @Override
    public LoginConfigs getById(Guid id) {
       return getCallsHandler().executeRead("GetloginconfigsById",loginConfigsRowMapper,createIdParameterMapper(id));
    }
    @Override
    public LoginConfigs getByName(String roleName) {
        MapSqlParameterSource parameterSource = getCustomMapSqlParameterSource().addValue("role_name", roleName);
        return getCallsHandler().executeRead("GetloginconfigsByName", loginConfigsRowMapper, parameterSource);
    }
    @Override
    public List<LoginConfigs> getAll() {
        return getCallsHandler().executeReadList("GetAllFromloginconfigs",loginConfigsRowMapper,getCustomMapSqlParameterSource());
    }
    private MapSqlParameterSource createIdParameterMapper(Guid id){
        return getCustomMapSqlParameterSource().addValue("loginconfig_id", id);
    }
    private MapSqlParameterSource createFullParametersMapper(LoginConfigs entity) {
        return createIdParameterMapper(entity.getId())
                .addValue("ip_limit",entity.getIpLimit())
                .addValue("mac_limit",entity.getMacLimit())
                .addValue("time_limit",entity.getTimeLimit())
                .addValue("role_name",entity.getRoleName())
                .addValue("other",entity.getOther());
    }
}

後端邏輯層

後端邏輯層是對操做命令化,把每種操做請求都封裝成命令的形式
[參數]ui

1. LoginConfigsOperationParameters 模塊操做的基本參數類
2. AddLoginConfigsCommand  添加操做的參數類
3. RemoveLoginConfigsParameters  更新操做的參數類
4. UpdateLoginConfigsParameters   刪除操做的參數類

【查詢與命令】this

1. GetAllLoginConfigsQuery   獲取全部登陸策略的查詢類
2. GetLoginConfigByIdQuery  根據id獲取指定的登陸策略的查詢類
3. GetLoginConfigByNameQuery  根據角色名獲取指定登陸策略的查詢類
4. AddLoginConfigsCommand  添加LoginConfigs的命令類
5. RemoveLoginConfigsCommand   刪除LoginConfigs的命令類
6. UpdateLoginConfigsCommand  更新LoginConfigs的命令類

【類型】日誌

1. VdcActionType  動做類型集合
2. VdcQueryType   查詢類型集合
3. VdcObjectType   對象類型集合
4. ActionGroup   動做組集合
//LoginConfigsOperationParameters.java  是全部操做的參數類的父類
public class LoginConfigsOperationParameters extends VdcActionParametersBase{
    private static final long serialVersionUID = 3991635679508928037L;
    @Valid
    private LoginConfigs loginConfigs;
    public LoginConfigsOperationParameters(){
    }
    public LoginConfigsOperationParameters(Guid id){
        this.setId(id);
        loginConfigs.setId(id);
    }
    public LoginConfigsOperationParameters(LoginConfigs _loginConfigs){
        loginConfigs = _loginConfigs;
    }
    public Guid getId(){
        return  loginConfigs == null ? null : loginConfigs.getId();
    }
    public  void setId(Guid id){
        if(loginConfigs == null){
            loginConfigs = new LoginConfigs();
        }
        loginConfigs.setId(id);
    }
    public String getName(){
        return loginConfigs == null ? null : loginConfigs.getRoleName();
    }
    public void setName(String name){
        if(loginConfigs == null){
            loginConfigs = new LoginConfigs();
        }else {
            loginConfigs.setRoleName(name);
        }
    }
    public LoginConfigs getLoginConfigs(){
        return loginConfigs;
    }
    public void setLoginConfigs(LoginConfigs loginConfigs){
        this.loginConfigs = loginConfigs;
    }
}

添加操做的參數類

public class AddLoginConfigsParameters extends LoginConfigsOperationParameters{
    private static final long serialVersionUID = -7832310521101821905L;

    public AddLoginConfigsParameters(LoginConfigs loginConfigs){
        super(loginConfigs);
    }
}

獲取全部登陸策略的查詢類

public class GetAllLoginConfigsQuery <P extends VdcQueryParametersBase> extends QueriesCommandBase<P>{

    public GetAllLoginConfigsQuery(P parameters) {
        super(parameters);
    }

    @Override
    protected void executeQueryCommand() {
        getQueryReturnValue().setReturnValue(getDbFacade().getLoginConfigsDao().getAll());
    }
}

添加登陸限制策略的命令類

public class AddLoginConfigsCommand <T extends AddLoginConfigsParameters> extends AbstractCommand<T> {
    public AddLoginConfigsCommand(T parameters, CommandContext cmdContext) {
        super(parameters, cmdContext);
    }

    public AddLoginConfigsCommand(Guid commandId) {
        super(commandId);
    }

    @Override
    protected void executeCommand() {
        DbFacade dbFacade = DbFacade.getInstance();
        getParameters().setId(Guid.newGuid());
        DbUser user = getCurrentUser();
        LoginConfigs configs = getParameters().getLoginConfigs();
        if (configs == null) {
            setSucceeded(false);
            return;
        }
        String roleName = configs.getRoleName() == null ? "" : configs.getRoleName();
        if (user != null) {
            if (user.getLoginName().equals("systemAdmin")) {
                setSucceeded(false);
            } else if (user.getLoginName().equals("securityAdmin")) {
                //保密員配置普通用戶角色的登陸策略
                if (roleName.contains("SystemAdmin") || roleName.contains("AuditAdmin")|| roleName.contains("UserRole")) {
                    dbFacade.getLoginConfigsDao().save(configs);
                    setSucceeded(true);
                }
            } else if (user.getLoginName().equals("auditAdmin")) {
                if (roleName.contains("SecurityAdmin")) {
                    dbFacade.getLoginConfigsDao().save(configs);
                    setSucceeded(true);
                }
            }
        } else {
            setSucceeded(false);
        }
    }
//檢測權限
    @Override
    public List<PermissionSubject> getPermissionCheckSubjects() {
        return Collections.singletonList(new PermissionSubject(Guid.SYSTEM,
                VdcObjectType.System, ActionGroup.LOGIN_CONFIG));
    }
//記錄日誌
    @Override
    public AuditLogType getAuditLogTypeValue() {
        return getSucceeded() ? AuditLogType.ADD_LOGIN_CONFIGS
                : AuditLogType.ADD_LOGIN_CONFIGS_ERROR;
    }
}

VdcQueryType.java 對新增的查詢類添加對應的枚舉類

GetAllLoginConfigs(RoleType.USER),
    GetLoginConfigById(RoleType.USER),
    GetLoginConfigByName(RoleType.USER),

VdcActionType 對新增的命令類添加對應的枚舉類

AddLoginConfigs(8004,ActionGroup.LOGIN_CONFIG,false,QuotaDependency.NONE),
    UpdateLoginConfigs(8005,ActionGroup.LOGIN_CONFIG,false,QuotaDependency.NONE),
    RemoveLoginConfigs(8006,ActionGroup.LOGIN_CONFIG,false,QuotaDependency.NONE),

ActionGroup 與角色權限有關

LOGIN_CONFIG(1701,RoleType.USER, false),

Rest接口層

BackendLoginConfigsResource 主資源實現類
BackendLoginConfigResource 子資源實現類
BackendApiResource 後臺應用類,用於分發請求
LoginConfigsResource 主資源接口類
LoginConfigResource 子資源接口類
SystemResource
LoginConfigsResource 主資源

@Produces(ApiMediaType.APPLICATION_JSON)
public interface LoginConfigsResource {
    @POST
    @Consumes(ApiMediaType.APPLICATION_JSON)
    default Response add(LoginConfigs loginConfigs) {
        throw new UnsupportedOperationException();
    }

    @GET
    default List<LoginConfigs> list() {
        throw new UnsupportedOperationException();
    }

    @POST
    @Consumes(ApiMediaType.APPLICATION_JSON)
    @Path("getByname")
    default LoginConfigs getByName (LoginConfigs loginConfigs) {
        throw new UnsupportedOperationException();
    }

    @Path("{id}")
    LoginConfigResource getLoginConfigResource(@PathParam("id") String id);
}

LoginConfigResource 子資源

@Produces({ApiMediaType.APPLICATION_XML, ApiMediaType.APPLICATION_JSON})
public interface LoginConfigResource {
    @DELETE
    default Response remove() {
        throw new UnsupportedOperationException();
    }

    @GET
    default LoginConfigs get() {
        throw new UnsupportedOperationException();
    }

    @PUT
    @Consumes(ApiMediaType.APPLICATION_JSON)
    default LoginConfigs update(LoginConfigs loginConfigs) {
        throw new UnsupportedOperationException();
    }
}

BackendLoginConfigsResource 主資源實現類

public class BackendLoginConfigsResource extends BackendResource implements LoginConfigsResource {
    @Override
    public Response add(LoginConfigs loginConfigs) {
        return performAction(VdcActionType.AddLoginConfigs,new AddLoginConfigsParameters(loginConfigs));
    }

    @Override
    public List<LoginConfigs> list() {
        VdcQueryReturnValue returnValue = runQuery(VdcQueryType.GetAllLoginConfigs, new VdcQueryParametersBase());
        return returnValue.getReturnValue();
    }

    @Override
    public LoginConfigs getByName(LoginConfigs loginConfigs) {
        String roleName = loginConfigs.getRoleName();
        VdcQueryReturnValue returnValue = runQuery(VdcQueryType.GetLoginConfigByName, sessionize(new NameQueryParameters(roleName)));
        return returnValue.getReturnValue();
    }

    @Override
    public LoginConfigResource getLoginConfigResource(String id) {
        return inject(new BackendLoginConfigResource(id));
    }
}

BackendLoginConfigsResource 子資源實現類

public class BackendLoginConfigResource extends BackendResource implements LoginConfigResource{
    protected Guid id;

    public BackendLoginConfigResource(String id){
        this.id = asGuid(id);
    }

    @Override
    public Response remove(){
        return performAction(VdcActionType.RemoveLoginConfigs,new RemoveLoginConfigsParameters(id));
    }

    @Override
    public LoginConfigs get(){
        VdcQueryReturnValue returnValue = runQuery(VdcQueryType.GetLoginConfigById, sessionize(new IdQueryParameters(id)));
        return returnValue.getReturnValue();
    }

    @Override
    public LoginConfigs update(LoginConfigs loginConfigs) {
        loginConfigs.setId(this.id);
        Response response = performAction(VdcActionType.UpdateLoginConfigs,new UpdateLoginConfigsParameters(loginConfigs));
        if (response.getStatus() == 200) {
            return runQuery(VdcQueryType.GetLoginConfigById, sessionize(new IdQueryParameters(id))).getReturnValue();
        }
        return null;
    }
}

systemResource

@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public interface SystemResource {
@Path("loginConfigs")
    LoginConfigsResource getLoginConfigsResource();
....
}

至此登陸限制策略的後臺接口已完成,可經過loginconfigs接口對登陸策略進行增刪改查。這樣實現起來簡單明瞭,不過因爲是根據角色定義登陸限制策略,同一種角色只有一種登陸限制策略,對不一樣用戶暫時不能作到多樣性控制,後期再修正。還有就是有多條限制策略時都存在同一張表的同一個字段中,前臺需作一些處理。

相關文章
相關標籤/搜索