【oVirt二次开发】管理员页面添加本地用户

需求:

  在没有AD域的时候,能够直接在管理员页面上进行添加本地用户,并能对本地用户进行编辑(用户名,密码等),删除操作;

社区的做法

   提供命令行在服务器后台上进行手动敲命令来添加,相关链接-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:

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值