关闭

在OpenCms中利用Java代码删除、创建、更新用户!

标签: javaexceptionusernull
842人阅读 评论(0) 收藏 举报
分类:

<%
    org.opencms.file.CmsObject cmsAdminObj
           = org.opencms.main.OpenCms.initCmsObject(
                 org.opencms.main.OpenCms.getDefaultUsers().getUserGuest()
             );
    cmsAdminObj.loginUser("Admin", "admin"); // 先以管理员身份登录,保证管理员的用户名和密码是正确的

    // 先检测该用户是否存在,存在则删除
    org.opencms.file.CmsUser user = null;
    try {
        user = cmsAdminObj.readUser("test2");
        cmsAdminObj.deleteUser("test2");
        out.println("The user test2 exists, and has already been deleted!");
    } catch (Exception e) {
    }

    // 如果该用户不存在,则创建该用户
    try {
        java.util.Hashtable params = new java.util.Hashtable();
        org.opencms.file.CmsUser newuser
            = cmsAdminObj.createUser("test2","12345678", "This is a new user", params);
        cmsAdminObj.addUserToGroup("test2","Administrators"); //optional
        out.println("The user has been created successfully!");
    } catch (Exception e) {
    }

    // 修改指定用户
    try {
        user = cmsAdminObj.readUser("test2");
        user.setFirstname("hello");
        user.setLastname("world");
        user.setDescription("This a new user:)");
        user.setEmail("test@test.com");
      
        // 重新设置密码
        cmsAdminObj.setPassword("test2","6666");

        // Updates the user information.
        cmsAdminObj.writeUser(user);

        out.println("The user has been modified!");
    } catch (Exception e) {
    }
 %>

原创地址:http://zcdxzsz.iteye.com/blog/155143
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:27031次
    • 积分:376
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:3篇
    • 译文:0篇
    • 评论:2条