天天看點

【oVirt二次開發】管理者頁面添加本地使用者

需求:

  在沒有AD域的時候,能夠直接在管理者頁面上進行添加本地使用者,并能對本地使用者進行編輯(使用者名,密碼等),删除操作;

社群的做法:

   提供指令行在伺服器背景上進行手動敲指令來添加,相關連結-oVirt本地使用者管理;

 缺陷:

   對客戶要求比較高,增加客戶的維護程度;

改進設計:

   能夠在管理者頁面上操作本地使用者,進行添加,編輯,删除等操作;

    支援使用者的單個、批量添加;

【oVirt二次開發】管理者頁面添加本地使用者

實作思路(以添加使用者為例):

  1. web頁面建立一個對話框,裡面包含建立本地使用者需要的一些屬性值

  2. 背景邏輯,a.拿到前端頁面上的屬性值,然後調用shell腳本;b.拿到前端頁面上的屬性值,然後調用建立本地使用者的接口函數;

實作步驟:

  1. 編寫web頁面,聲明一個xml檔案-AddLocalUserPopupView.ui.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
             xmlns:g="urn:import:com.google.gwt.user.client.ui" xmlns:d="urn:import:org.ovirt.engine.ui.common.widget.dialog"
             xmlns:ge="urn:import:org.ovirt.engine.ui.common.widget.editor.generic">

    <ui:style>

        .tab {
            height: 40px;
            margin-top: 2px;
        }

        .userConfirmPasswordEditor {
            display: none;
        }
    </ui:style>
    <d:SimpleDialogPanel width="400px" height="325px">
        <d:content>
            <g:FlowPanel>
                <ge:StringEntityModelTextBoxEditor ui:field="localUserNameEditor" addStyleNames="{style.tab}"/>
                <ge:StringEntityModelTextBoxEditor ui:field="localUserFirstNameEditor" addStyleNames="{style.tab}"/>
                <ge:StringEntityModelTextBoxEditor ui:field="localUserLastNameEditor" addStyleNames="{style.tab}"/>
                <ge:IntegerEntityModelTextBoxEditor ui:field="totalNumbersEditor" addStyleNames="{style.tab}"/>
                <ge:StringEntityModelPasswordBoxEditor ui:field="localUserPasswordEditor" addStyleNames="{style.tab}"/>
                <ge:StringEntityModelPasswordBoxEditor ui:field="localUserConfirmPasswordEditor" addStyleNames="{style.tab}"/>
                <ge:StringEntityModelTextBoxEditor ui:field="emailEditor" addStyleNames="{style.tab}"/>
            </g:FlowPanel>
        </d:content>
    </d:SimpleDialogPanel>
</ui:UiBinder>
           

2. 定義一個相關的類與xml檔案對應

public class AddLocalUserPopupView extends AbstractModelBoundPopupView<LocalUserModel> implements AddLocalUserPopupPresenterWidget.ViewDef {

    interface Driver extends SimpleBeanEditorDriver<LocalUserModel, AddLocalUserPopupView> {
    }

    interface ViewUiBinder extends UiBinder<SimpleDialogPanel, AddLocalUserPopupView> {
        ViewUiBinder uiBinder = GWT.create(ViewUiBinder.class);
    }

    interface ViewIdHandler extends ElementIdHandler<AddLocalUserPopupView> {
        ViewIdHandler idHandler = GWT.create(ViewIdHandler.class);
    }
    private final Driver driver = GWT.create(Driver.class);
    private final static ApplicationConstants constants = AssetProvider.getConstants();

    @UiField
    @Path(value = "userLoginName.entity")
    @WithElementId
    StringEntityModelTextBoxEditor localUserNameEditor;

    @UiField
    @Path(value = "userFirstName.entity")
    @WithElementId
    StringEntityModelTextBoxEditor localUserFirstNameEditor;

    @UiField
    @Path(value = "userLastName.entity")
    @WithElementId
    StringEntityModelTextBoxEditor localUserLastNameEditor;

    @UiField
    @Path(value = "userPassWD.entity")
    @WithElementId
    StringEntityModelPasswordBoxEditor localUserPasswordEditor;

    @UiField
    @Path(value = "userConfirmPassWD.entity")
    @WithElementId
    StringEntityModelPasswordBoxEditor localUserConfirmPasswordEditor;

    @UiField
    @Path(value = "userEmail.entity")
    @WithElementId
    StringEntityModelTextBoxEditor emailEditor;

    @UiField
    @Path(value = "totalNumbers.entity")
    @WithElementId
    IntegerEntityModelTextBoxEditor totalNumbersEditor;

    @Inject
    public AddLocalUserPopupView(EventBus eventBus) {
        super(eventBus);
        initWidget(ViewUiBinder.uiBinder.createAndBindUi(this));
        ViewIdHandler.idHandler.generateAndSetIds(this);
        localize();
        driver.initialize(this);
    }

    private void localize() {
        localUserNameEditor.setLabel(constants.localUserNameEditor());
        localUserFirstNameEditor.setLabel(constants.localUserFirstNameEditor());
        localUserLastNameEditor.setLabel(constants.localUserLastNameEditor());
        localUserPasswordEditor.setLabel(constants.localUserPasswordEditor());
        localUserConfirmPasswordEditor.setLabel(constants.localUserConfirmPasswordEditor());
        emailEditor.setLabel(constants.emailEditor());
        totalNumbersEditor.setLabel(constants.localUserNumbers());
    }

    @Override
    public void focusInput() {
        localUserNameEditor.setFocus(true);
    }

    @Override
    public void edit(LocalUserModel object) {
        driver.edit(object);
    }

    @Override
    public LocalUserModel flush() {
        return driver.flush();
    }

    @Override
    public void hideTotalNumbers(Boolean value) {
        totalNumbersEditor.setVisible(value);
    }
}
           

3. 在業務邏輯層(bll)增加添加使用者的邏輯處理

+public class AddLocalUserCommand<T extends AddLocalUserParameters> extends CommandBase<T> {

    public AddLocalUserCommand(T params) {
        this(params, null);
    }

    public AddLocalUserCommand(T params, CommandContext commandContext) {
        super(params, commandContext);
    }

    private static final Logger log = LoggerFactory.getLogger(AddLocalUserCommand.class);

    private String USER_BASE_CMD = "ovirt-aaa-jdbc-tool ";
    private String USER_ADD_CMD = "user add ";
    private String USER_ENABLED = "user edit ";
    private String USER_UNLOCK = "user unlock ";
    private String USER_SHOW = "user show ";
    private String ENABLED_FLAG = " --flag=-disabled";
    private String USER_ADD_ATTRIBUTE = " --attribute=";
    @Override
    protected void executeCommand() {
        String loginName = getParameters().getLoginName();
        String firstName = getParameters().getFirstName();
        String lastName = getParameters().getLastName();
        String passWD = getParameters().getUserPassWD();
        String email = getParameters().getUserEmail();

        String exec_add_cmd = USER_BASE_CMD + USER_ADD_CMD + loginName;
        if (firstName != null) {
            exec_add_cmd = exec_add_cmd + USER_ADD_ATTRIBUTE + "firstName=" + firstName;
        }
        if (lastName != null) {
            exec_add_cmd = exec_add_cmd + USER_ADD_ATTRIBUTE + "lastName=" + lastName;
        }
        if (email != null) {
            exec_add_cmd = exec_add_cmd + USER_ADD_ATTRIBUTE + "email=" + email;
        }
        try {
            Process userAddProcess = Runtime.getRuntime().exec(exec_add_cmd);
            int retVal = userAddProcess.waitFor();
            if (retVal == 0) {
                String exec_enabled_cmd = USER_BASE_CMD + USER_ENABLED + loginName + ENABLED_FLAG;
                Process userEnabledProcess = Runtime.getRuntime().exec(exec_enabled_cmd);

                String exec_unlock_cmd = USER_BASE_CMD + USER_UNLOCK + loginName;
                Process userUnlockProcess = Runtime.getRuntime().exec(exec_unlock_cmd);

                String exec_passwd_reset = PASSWD_RESET + loginName + " " + passWD;
                Process userPasswdResetProcess = Runtime.getRuntime().exec(exec_passwd_reset);
                retVal = userPasswdResetProcess.waitFor();
                if (retVal == 0) {
                    log.info("Success add local user '{}'", loginName);
                    setSucceeded(true);
                } else {
                    log.error("Faild to add local user '{}'", loginName);
                    setSucceeded(false);
                }
            } else {
                log.error("Faild to add local user '{}'", loginName);
                setSucceeded(false);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
           

NOTE: