批量授一种权限给用户程序 plm enovia

set context user DanielDai;
add program LUX_UserAssignment
java
execute immediate
code 'import com.matrixone.apps.domain.util.MqlUtil; import matrix.db.Context; import org.apache.commons.lang.StringUtils; import java.io.*; public class ${CLASSNAME} { public void assignRole(Context context, String[] args) throws Exception{ if(!StringUtils.isNotBlank(args[0])){ System.out.println("exit0:path is null"); return; } String path = args[0]; if(!StringUtils.isNotBlank(args[1])){ System.out.println("exit1:role is null"); return; } String role = args[1]; File file = new File(path); if(!file.exists()){ System.out.println("exist2:file no exist"); return; } InputStream inputStream = new FileInputStream(file); Reader reader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(reader); String line = null; while((line=bufferedReader.readLine())!=null){ String mql = "temp query bus Person * - where attribute[Email Address]==" + line + " ;"; String res = MqlUtil.mqlCommand(context,mql); if(!StringUtils.isNotBlank(res)){ String[] resS = res.split(","); String personName = resS[1]; mql = "modify Person " + personName + "assign role " + role + " ;"; MqlUtil.mqlCommand(context,mql); } } } } '
;
compile program LUX_UserAssignment force;
execute program LUX_UserAssignment -method assignRole '/opt/plm/MatrixWorkArea/daniel/UserList.csv' 'LUX_CAD_Download' ;
delete program LUX_UserAssignment;

 

1.UserList.csv 中必须是用户的邮箱列表,因为用户的邮箱唯一,并且区分大小写。

改变 assignRole method 后面的路径和权限名就可以了。

命令中的java 代码 格式化之后:

  

import com.matrixone.apps.domain.util.MqlUtil;
import matrix.db.Context;
import org.apache.commons.lang.StringUtils;
import java.io.*;
public class LUX_UserAssignment_mxJPO {
public void assignRole(Context context, String[] args) throws Exception{
if(!StringUtils.isNotBlank(args[0])){

System.out.println("exit0:path is null");
return;
}
String path = args[0];
if(!StringUtils.isNotBlank(args[1])){
System.out.println("exit1:role is null");
return;
}
String role = args[1];
File file = new File(path);
if(!file.exists()){
System.out.println("exist2:file does not exist");
return;
}
InputStream inputStream = new FileInputStream(file);
Reader reader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(reader);
String line = null;
while(StringUtils.isNotBlank(line=bufferedReader.readLine())){
String mql = "temp query bus Person * - where \"attribute[Email Address]==" + line + "\" ;";
String res = MqlUtil.mqlCommand(context,mql);
if(!StringUtils.isNotBlank(res)){
System.out.println("Warning:User with email(" + line + ") does not exist!");
continue;
}
if(StringUtils.isNotBlank(res)){
String[] resS = res.split(" ");
String personName = resS[1];
mql = "modify Person " + personName + " assign role " + role + " ;";
MqlUtil.mqlCommand(context,mql);
System.out.println(role + " is assigned to User " + personName + "(" + line + ")" + " successfully!");
}
}
}
}
posted @ 2019-12-16 00:51  danieldai  阅读(283)  评论(0编辑  收藏  举报