Package org.exist.xmldb

Examples of org.exist.xmldb.UserManagementService


    return true;
  }
 
  private Account[] getUsers()
  {
    final UserManagementService service = getUserService();
    if(service == null)
      {return new Account[0];}
    try
    {
      return service.getAccounts();
    }
    catch (final XMLDBException xe)
    {
      ClientFrame.showErrorMessage("Could not get list of users: user attributes will be invalid", xe);
      return new Account[0];
View Full Code Here


    protected void getResources() throws XMLDBException {
        if (current == null) {
            return;
        }
        setProperties();
        final UserManagementService mgtService = (UserManagementService) current
                .getService("UserManagementService", "1.0");
        final String childCollections[] = current.listChildCollections();
        final String childResources[] = current.listResources();
       
        resources = new String[childCollections.length + childResources.length];
        //Collection child;
        Permission perm;
       
        final List<ResourceDescriptor> tableData = new ArrayList<ResourceDescriptor>(resources.length); // A list of ResourceDescriptor for the GUI
       
        final String cols[] = new String[4];
        int i = 0;
        for (; i < childCollections.length; i++) {
            //child = current.getChildCollection(childCollections[i]);
           
            perm = mgtService.getSubCollectionPermissions(current, childCollections[i]);
           
            if ("true".equals(properties.getProperty("permissions"))) {
                cols[0] = perm.toString();
              cols[1] = getOwnerName(perm);
              cols[2] = getGroupName(perm);
                cols[3] = URIUtils.urlDecodeUtf8(childCollections[i]);
                resources[i] = 'd' + formatString(cols, colSizes);
            } else {
                resources[i] = URIUtils.urlDecodeUtf8(childCollections[i]);
            }

            final Date created = mgtService.getSubCollectionCreationTime(current, childCollections[i]);

            if (startGUI) {
                tableData.add( new ResourceDescriptor.Collection(
                        XmldbURI.create(childCollections[i]),
                        getOwnerName(perm),
                        getGroupName(perm),
                        "c" + ((perm instanceof ACLPermission && ((ACLPermission)perm).getACECount() > 0) ? perm.toString() + '+' : perm.toString()),
                        created ) );
            }
            completitions.add(childCollections[i]);
        }
        Resource res;
        for (int j = 0; j < childResources.length; i++, j++) {
            res = current.getResource(childResources[j]);
            perm = mgtService.getPermissions(res);
            if (perm == null) {
                System.out.println("null"); //TODO this is not useful!
            }
            if ("true".equals(properties.getProperty("permissions"))) {
                resources[i] = '-' + perm.toString() + '\t' + perm.getOwner().getName()
View Full Code Here

                if (startGUI) {
                    messageln("command not supported in GUI mode. Please use the \"Edit users\" menu option.");
                    return true;
                }
                try {
                    final UserManagementService mgtService = (UserManagementService) current
                            .getService("UserManagementService", "1.0");
                   
                    String p1;
                    String p2;
                    while (true) {
                        p1 = console.readLine("password: ", Character.valueOf('*'));
                        p2 = console.readLine("re-enter password: ", Character.valueOf('*'));
                        if (p1.equals(p2)) {
                            break;
                        }
                        System.out.println(EOL + "entered passwords differ. Try again...");
                       
                    }
                    final UserAider user = new UserAider(args[1]);
                    user.setPassword(p1);
                    final String groups = console.readLine("enter groups: ");
                    final StringTokenizer tok = new StringTokenizer(groups, " ,");
                    while (tok.hasMoreTokens()) {
                        final String group = tok.nextToken();
                        if (group.length() > 0) {
                            user.addGroup(group);
                        }
                    }
                   
                    mgtService.addAccount(user);
                    System.out.println("user " + user + " created.");
                } catch (final Exception e) {
                    System.out.println("ERROR: " + e.getMessage());
                    e.printStackTrace();
                }
            } else if (args[0].equalsIgnoreCase("users")) {
                final UserManagementService mgtService = (UserManagementService) current
                        .getService("UserManagementService", "1.0");
                final Account users[] = mgtService.getAccounts();
                System.out.println("User\t\tGroups");
                System.out.println("-----------------------------------------");
                for (int i = 0; i < users.length; i++) {
                    System.out.print(users[i].getName() + "\t\t");
                    final String[] groups = users[i].getGroups();
                    for (int j = 0; j < groups.length; j++) {
                        System.out.print(groups[j]);
                        if (j + 1< groups.length) {
                            System.out.print(", ");
                        }
                    }
                    System.out.println();
                }
            } else if (args[0].equalsIgnoreCase("passwd")) {
                if (startGUI) {
                    messageln("command not supported in GUI mode. Please use the \"Edit users\" menu option.");
                    return true;
                }
                if (args.length < 2) {
                    System.out.println("Usage: passwd username");
                    return true;
                }
                try {
                    final UserManagementService mgtService = (UserManagementService) current
                            .getService("UserManagementService", "1.0");
                    final Account user = mgtService.getAccount(args[1]);
                    if (user == null) {
                        System.out.println("no such user.");
                        return true;
                    }
                    String p1;
                    String p2;
                    while (true) {
                        p1 = console.readLine("password: ", Character.valueOf('*'));
                        p2 = console.readLine("re-enter password: ", Character.valueOf('*'));
                        if (p1.equals(p2)) {
                            break;
                        }
                        System.out.println(EOL + "entered passwords differ. Try again...");
                    }
                    user.setPassword(p1);
                    mgtService.updateAccount(user);
                    properties.setProperty("password", p1);
                } catch (final Exception e) {
                    System.err.println("ERROR: " + e.getMessage());
                }
            } else if (args[0].equalsIgnoreCase("chmod")) {
                if (args.length < 2) {
                    System.out.println("Usage: chmod [resource] mode");
                    return true;
                }
               
                final Collection temp;
                if (args.length == 3) {
                    System.out.println("trying collection: " + args[1]);
                    temp = current.getChildCollection(args[1]);
                    if (temp == null) {
                        System.out.println(EOL + "trying resource: " + args[1]);
                        final Resource r = current.getResource(args[1]);
                        if (r != null) {
                            final UserManagementService mgtService = (UserManagementService) current
                                .getService("UserManagementService", "1.0");
                            mgtService.chmod(r, args[2]);
                        } else {
                            System.err.println("Resource " + args[1]
                                    + " not found.");
                        }
                    } else {
                        final UserManagementService mgtService = (UserManagementService) temp
                            .getService("UserManagementService", "1.0");
                        mgtService.chmod(args[2]);
                    }
                } else {
                    final UserManagementService mgtService = (UserManagementService) current
                        .getService("UserManagementService", "1.0");
                    mgtService.chmod(args[1]);
                }
                // re-read current collection
                current = DatabaseManager.getCollection(properties
                    .getProperty("uri")
                    + path, properties.getProperty("user"), properties
                    .getProperty("password"));
                getResources();
            } else if (args[0].equalsIgnoreCase("chown")) {
                if (args.length < 3) {
                    System.out
                            .println("Usage: chown username group [resource]");
                    return true;
                }
               
                final Collection temp;
                if (args.length == 4) {
                    temp = current.getChildCollection(args[3]);
                } else {
                    temp = current;
                }
                if (temp != null) {
                    final UserManagementService mgtService = (UserManagementService) temp
                            .getService("UserManagementService", "1.0");
                    final Account u = mgtService.getAccount(args[1]);
                    if (u == null) {
                        System.out.println("unknown user");
                        return true;
                    }
                    mgtService.chown(u, args[2]);
                    System.out.println("owner changed.");
                    getResources();
                    return true;
                }
                final Resource res = current.getResource(args[3]);
                if (res != null) {
                    final UserManagementService mgtService = (UserManagementService) current
                            .getService("UserManagementService", "1.0");
                    final Account u = mgtService.getAccount(args[1]);
                    if (u == null) {
                        System.out.println("unknown user");
                        return true;
                    }
                    mgtService.chown(res, u, args[2]);
                    getResources();
                    return true;
                }
                System.err.println("Resource " + args[3] + " not found.");
               
            } else if (args[0].equalsIgnoreCase("lock") || args[0].equalsIgnoreCase("unlock")) {
                if(args.length < 2) {
                    messageln("Usage: lock resource");
                    return true;
                }
                final Resource res = current.getResource(args[1]);
                if (res != null) {
                    final UserManagementService mgtService = (UserManagementService)
                    current.getService("UserManagementService", "1.0");
                    final Account user = mgtService.getAccount(properties.getProperty("user", "guest"));
                    if(args[0].equalsIgnoreCase("lock")) {
                        mgtService.lockResource(res, user);
                    } else {
                        mgtService.unlockResource(res);
                    }
                }
               
            } else if (args[0].equalsIgnoreCase("elements")) {
                System.out.println("Element occurrences in collection "
                        + current.getName());
                System.out
                        .println("--------------------------------------------"
                        + "-----------");
                final IndexQueryService service = (IndexQueryService) current
                        .getService("IndexQueryService", "1.0");
                final Occurrences[] elements = service.getIndexedElements(true);
                for (int i = 0; i < elements.length; i++) {
                    System.out
                            .println(formatString(elements[i].getTerm().toString(),
                            Integer.toString(elements[i]
                            .getOccurrences()), 50));
                }
                return true;
               
            } else if (args[0].equalsIgnoreCase("terms")) {
                if (args.length < 3) {
                    System.out
                            .println("Usage: terms [xpath] sequence-start sequence-end");
                    return true;
                }
                final IndexQueryService service = (IndexQueryService) current
                        .getService("IndexQueryService", "1.0");
                Occurrences[] terms;
                if (args.length == 3) {
                    terms = service.scanIndexTerms(args[1], args[2], true);
                } else {
                    terms = service.scanIndexTerms(args[1], args[2], args[3]);
                }
                System.out.println("Element occurrences in collection " + current.getName());
                System.out.println("-------------------------------------------------------");
                for (int i = 0; i < terms.length; i++) {
                    System.out.println(formatString(terms[i].getTerm().toString(), Integer.toString(terms[i].getOccurrences()), 50));
                }
            } else if (args[0].equalsIgnoreCase("xupdate")) {
                if (startGUI) {
                    messageln("command not supported in GUI mode.");
                    return true;
                }
                String lastLine, command = "";
                try {
                    while (true) {
                        lastLine = console.readLine("| ");
                        if (lastLine == null || lastLine.length() == 0) {
                            break;
                        }
                        command += lastLine;
                    }
                } catch (final EOFException e) {
                    //TODO report error?
                } catch (final IOException e) {
                    //TODO report error?
                }
                final String xupdate = "<xu:modifications version=\"1.0\" "
                        + "xmlns:xu=\"http://www.xmldb.org/xupdate\">"
                        + command + "</xu:modifications>";
                final XUpdateQueryService service = (XUpdateQueryService) current
                        .getService("XUpdateQueryService", "1.0");
                final long mods = service.update(xupdate);
                System.out.println(mods + " modifications processed.");
               
            } else if (args[0].equalsIgnoreCase("map")) {
                final StringTokenizer tok = new StringTokenizer(args[1], "= ");
                final String prefix;
                if (args[1].startsWith("=")) {
                    prefix = "";
                } else {
                    if (tok.countTokens() < 2) {
                        messageln("please specify a namespace/prefix mapping as: prefix=namespaceURI");
                        return true;
                    }
                    prefix = tok.nextToken();
                }
                final String uri = tok.nextToken();
                namespaceMappings.put(prefix, uri);
               
            } else if (args[0].equalsIgnoreCase("set")) {
                if (args.length == 1) {
                    properties.list(System.out);
                } else {
                    try {
                        final StringTokenizer tok = new StringTokenizer(args[1], "= ");
                        if (tok.countTokens() < 2) {
                            System.err.println("please specify a key=value pair");
                            return true;
                        }
                        final String key = tok.nextToken();
                        final String val = tok.nextToken();
                        properties.setProperty(key, val);
                        current.setProperty(key, val);
                        getResources();
                    } catch (final Exception e) {
                        System.err.println("Exception: " + e.getMessage());
                    }
                }
            } else if (args[0].equalsIgnoreCase("shutdown")) {
                final DatabaseInstanceManager mgr = (DatabaseInstanceManager) current
                    .getService("DatabaseInstanceManager", "1.0");
                if (mgr == null) {
                    messageln("Service is not available");
                    return true;
                }
                mgr.shutdown();
                return true;
            } else if (args[0].equalsIgnoreCase("help") || "?".equals(args[0])) {
                displayHelp();
            } else if (args[0].equalsIgnoreCase("quit")) {
                return false;
                //XXX:make it pluggable
            } else if (havePluggableCommands) {
                final CollectionManagementServiceImpl mgtService = (CollectionManagementServiceImpl) current.getService("CollectionManagementService", "1.0");
                mgtService.runCommand(args);
                //****************************************************************
            } else {
                messageln("unknown command");
                return true;
            }
View Full Code Here

    }

    @Override
    public void apply() {
        try {
            final UserManagementService service = (UserManagementService)getTarget().getParentCollection().getService("UserManagementService", "1.0");
            final Permission permissions = service.getPermissions(getTarget());
            service.setPermissions(getTarget(), getOwner(), getGroup(), getMode(), getAces()); //persist
        } catch(final XMLDBException xe) {
            String name = "unknown";
            try { name = getTarget().getId(); } catch(final XMLDBException x) { LOG.error(x.getMessage(), x); }
            final String msg = "ERROR: Failed to set permissions on Document '" + name + "'.";
            LOG.error(msg, xe);
View Full Code Here

    @Override
    public void apply() {
        try {

            UserManagementService service;
            if(getTarget().getName().equals(XmldbURI.ROOT_COLLECTION)) {
                service = (UserManagementService)getTarget().getService("UserManagementService", "1.0");
            } else {
                final Collection parent = getTarget().getParentCollection();
                service = (UserManagementService)parent.getService("UserManagementService", "1.0");
            }

            service.setPermissions(getTarget(), getOwner(), getGroup(), getMode(), getAces()); //persist
        } catch (final XMLDBException xe) {
            String name = "unknown";
            try { name = getTarget().getName(); } catch(final XMLDBException x) { LOG.error(x.getMessage(), x); }
            final String msg = "ERROR: Failed to set permissions on Collection '" + name + "'.";
            LOG.error(msg, xe);
View Full Code Here

        } else {
            dbUri = XmldbURI.xmldbUriFor(uri);
        }
       
        final Collection root = DatabaseManager.getCollection(dbUri.toString(), username, password);
        final UserManagementService mgmt = (UserManagementService)root.getService("UserManagementService", "1.0");
        final Account dba  = mgmt.getAccount(SecurityManager.DBA_USER);
        if (dba == null) {
            throw new XMLDBException(ErrorCodes.PERMISSION_DENIED, "'"+SecurityManager.DBA_USER+"' account can't be found.");
        }
        dba.setPassword(adminPassword);
        mgmt.updateAccount(dba);

        return adminPassword;
    }
View Full Code Here

      return new Account[0];
    }
  }
  private String[] getGroups()
  {
    final UserManagementService service = getUserService();
    if(service == null)
      {return new String[0];}
    try
    {
      return service.getGroups();
    }
    catch (final XMLDBException xe)
    {
      ClientFrame.showErrorMessage("Could not get list of groups: group attributes will be invalid", xe);
      return new String[0];
View Full Code Here

              {setText((String) resource.getContent());}
          else
              {setText(new String((byte[]) resource.getContent()));}
         
          // lock the resource for editing
          final UserManagementService service = (UserManagementService)
          client.current.getService("UserManagementService", "1.0"); //$NON-NLS-1$ //$NON-NLS-2$
          final Account user = service.getAccount(properties.getProperty("user")); //$NON-NLS-1$
          final String lockOwner = service.hasUserLock(resource);
          if(lockOwner != null) {
              if(JOptionPane.showConfirmDialog(this,
                      Messages.getString("DocumentView.6") + lockOwner + //$NON-NLS-1$
                      Messages.getString("DocumentView.7"), //$NON-NLS-1$
                      Messages.getString("DocumentView.8"), //$NON-NLS-1$
                      JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
                  dispose();
                  this.setCursor(Cursor.getDefaultCursor());
                  return;
              }
          }
         
          try {
              service.lockResource(resource, user);
          } catch(final XMLDBException ex) {
              System.out.println(ex.getMessage());
              JOptionPane.showMessageDialog(this,
                      Messages.getString("DocumentView.9")); //$NON-NLS-1$
              setReadOnly();
View Full Code Here

  {
    if (readOnly)
      {return;}
    try
    {
      final UserManagementService service = (UserManagementService) collection
          .getService("UserManagementService", "1.0"); //$NON-NLS-1$ //$NON-NLS-2$
      service.unlockResource(resource);
    }
    catch (final XMLDBException e)
    {
      e.printStackTrace();
    }
View Full Code Here

        assertEquals(RealmImpl.INITIAL_LAST_ACCOUNT_ID + 2, Integer.parseInt(result.getResource(0).getContent().toString())); //last account id should be that of 'jack'

        //create a test collection and give everyone access
        final CollectionManagementService cms = (CollectionManagementService)root.getService("CollectionManagementService", "1.0");
        final Collection test = cms.createCollection("test");
        final UserManagementService testUms = (UserManagementService)test.getService("UserManagementService", "1.0");
        testUms.chmod("rwxrwxrwx");

        //create and store a new document as 'frank'
        final Collection frankTest = DatabaseManager.getCollection("xmldb:exist:///db/test", FRANK_USER, FRANK_USER);
        final String FRANKS_DOCUMENT = "franks-document.xml";
        final Resource frankDoc = frankTest.createResource(FRANKS_DOCUMENT, XMLResource.RESOURCE_TYPE);
        frankDoc.setContent("<hello>frank</hello>");
        frankTest.storeResource(frankDoc);

        //create and store a new document as 'jack'
        final Collection jackTest = DatabaseManager.getCollection("xmldb:exist:///db/test", JACK_USER, JACK_USER);
        final String JACKS_DOCUMENT = "jacks-document.xml";
        final Resource jackDoc = jackTest.createResource(JACKS_DOCUMENT, XMLResource.RESOURCE_TYPE);
        jackDoc.setContent("<hello>jack</hello>");
        jackTest.storeResource(jackDoc);

        //restore the database backup
        final Restore restore = new Restore();
        restore.restore(new NullRestoreListener(), "admin", "", null, backupFile, "xmldb:exist:///db");


        //check the current user accounts after the restore
        result = xqs.query(accountQuery);
        assertUser(RealmImpl.ADMIN_ACCOUNT_ID, SecurityManager.DBA_USER, ((XMLResource) result.getResource(0)).getContentAsDOM());
        assertUser(RealmImpl.GUEST_ACCOUNT_ID, SecurityManager.GUEST_USER, ((XMLResource) result.getResource(1)).getContentAsDOM());
        assertUser(RealmImpl.INITIAL_LAST_ACCOUNT_ID + 1, "frank", ((XMLResource) result.getResource(2)).getContentAsDOM());
        assertUser(RealmImpl.INITIAL_LAST_ACCOUNT_ID + 2, "jack", ((XMLResource) result.getResource(3)).getContentAsDOM());
        assertUser(RealmImpl.INITIAL_LAST_ACCOUNT_ID + 4, "joe", ((XMLResource) result.getResource(4)).getContentAsDOM()); //this is `+ 4` because pre-allocating an id skips one

        //check the last user id after the restore
        result = xqs.query(lastAccountIdQuery);
        assertEquals(RealmImpl.INITIAL_LAST_ACCOUNT_ID + 4, Integer.parseInt(result.getResource(0).getContent().toString())); //last account id should be that of 'joe'

        //check the owner of frank's document after restore
        final Resource fDoc = test.getResource(FRANKS_DOCUMENT);
        final Permission franksDocPermissions = testUms.getPermissions(fDoc);
        assertEquals(FRANK_USER, franksDocPermissions.getOwner().getName());

        //check the owner of jack's document after restore
        final Resource jDoc = test.getResource(JACKS_DOCUMENT);
        final Permission jacksDocPermissions = testUms.getPermissions(jDoc);
        assertEquals(JACK_USER, jacksDocPermissions.getOwner().getName());
    }
View Full Code Here

TOP

Related Classes of org.exist.xmldb.UserManagementService

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.