需求:
在没有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: