package org.basex.core.proc;

import static org.basex.core.Text.*;
import java.io.IOException;
import org.basex.core.Main;
import org.basex.core.Proc;
import org.basex.core.User;
import org.basex.core.Commands.CmdPerm;
import org.basex.data.Data;

/**
 * Evaluates the 'create user' command and creates a new user.
 *
 * @author Workgroup DBIS, University of Konstanz 2005-09, ISC License
 * @author Christian Gruen
 */
public abstract class AAdmin extends Proc {
  /**
   * Default constructor.
   * @param a arguments
   */
  protected AAdmin(final String... a) {
    super(User.ADMIN, a);
  }

  /**
   * Returns the permissions for the specified admin process.
   * @param set flag for setting/removing permission
   * @return permissions or -1
   */
  protected boolean perm(final boolean set) {
    if(args[1].equals(ADMIN)) return error(USERADMIN);

    final String db = args[2];

    // find permission
    final CmdPerm cmd = getOption(CmdPerm.class);
    int perm = -1;
    if(cmd == CmdPerm.READ) {
      perm = User.READ;
    } else if(cmd == CmdPerm.WRITE) {
      perm = User.WRITE;
    } else if(cmd == CmdPerm.CREATE && db == null) {
      perm = User.CREATE;
    } else if(cmd == CmdPerm.ADMIN && db == null) {
      perm = User.ADMIN;
    } else if(cmd == CmdPerm.ALL) {
      perm = db == null ? User.READ | User.WRITE | User.CREATE | User.ADMIN :
        User.READ | User.WRITE;
    }
    if(perm == -1) return error(PERMINV);

    final User user = context.users.get(args[1]);
    if(user == null) return error(USERNO, args[1]);

    if(db == null) {
      // global permissions
      user.perm(set, perm);
      context.users.write();
    } else {
      try {
        final Data data = Open.open(context, db);
        User u = data.meta.users.get(args[1]);
        // add local user reference
        if(u == null) {
          u = user.copy();
          u.perm(false, User.ADMIN);
          u.perm(false, User.CREATE);
          if(perm == 1) u.perm(false, User.WRITE);
          if(perm == 2) u.perm(false, User.READ);
          data.meta.users.add(u);
        }
        u.perm(set, perm);
        data.flush();
        Close.close(context, data);
      } catch(final IOException ex) {
        Main.debug(ex);
        final String msg = ex.getMessage();
        return msg.isEmpty() ? error(DBOPENERR, db) : error(msg);
      }
    }
    return info(PERMUP);
  }
}

@@ -63,16 +63,18 @@
 ext.*;%0D%0A
+%0D%0A
 import j
@@ -250,16 +250,56 @@
 a.Data;%0D
+%0Aimport org.basex.server.ServerProcess;%0D
 %0A%0D%0A/**%0D%0A
@@ -1660,24 +1660,206 @@
 == null) %7B%0D%0A
+      // check if user is logged in%0D%0A      for(final ServerProcess s : context.sessions) %7B%0D%0A        if(s.context.user.name.equals(args%5B1%5D)) return error(USERLOG, args%5B1%5D);%0D%0A      %7D%0D%0A
       // glo

check if user is logged in at setting global permissions