Package com.zaranux.os.client.access

Source Code of com.zaranux.os.client.access.ACL

package com.zaranux.os.client.access;

import com.zaranux.client.api.AsyncCallback;
import com.zaranux.client.api.exceptions.ResourceNotFound;
import com.zaranux.client.api.types.Access;
import com.zaranux.client.java.util.Properties;

import com.zaranux.client.java.io.File;
import com.zaranux.client.java.io.FileInputStream;
import com.zaranux.client.java.io.FileOutputStream;

import java.util.Set;

import com.allen_sauer.gwt.log.client.Log;

public class ACL {

  public static void getACL(final String path, final AsyncCallback<ACL> callback)
  {
    getACL(new File(path), callback);
  }
  public static void getACL(final File file, final AsyncCallback<ACL> callback)
  {
    // check if the corresponding resource exists.
    file.exists(new AsyncCallback<Boolean>()
        {
        public void onSuccess(Boolean exists)
        {
          if(! exists)
          {
            callback.onFailure(new ResourceNotFound());
            return;
          }

          String name = file.getName();
          final String aclDirPath = file.getParent() + "/.zaranux/.meta/";
          String aclPath =  aclDirPath + name + ".zconfig";
         
          final File aclFile = new File(aclPath);
         
          // see if the resource has already an acl;
          aclFile.exists(new AsyncCallback<Boolean>()
          {
            public void onSuccess(Boolean b)
            {
              if(b) // resource has an acl
              {
              // load it
              FileInputStream fis = new FileInputStream(aclFile);
             
              final Properties properties = new Properties();
              properties.load(fis, new AsyncCallback<Boolean>()
                  {
                  public void onSuccess(Boolean b)
                  {
                    if(properties.get("@public") == null)
                      properties.put("@public", "");
                    if(properties.get("@users") == null)
                      properties.put("@users", "");
                    callback.onSuccess(new ACL(file,properties,aclFile));
                  }
                  public void onFailure(Throwable t)
                  {
                    callback.onFailure(t);
                  }
                  });
              }else
              {
                final File metaDir = new File(aclDirPath);
                // acl does not exist. check if meta dir exists
               
                metaDir.exists(new AsyncCallback<Boolean>()
                    {
                     
                      public void onSuccess(Boolean b)
                      {
                        if(b)// meta dir exists
                        {
                          // return and empty property
                          Properties properties = new Properties();
                          properties.put("@public", "");
                          properties.put("@users", "");
                         
                          callback.onSuccess(new ACL(file,properties,aclFile));
                        }else
                        {
                          metaDir.mkdirs(new AsyncCallback<Boolean>()
                              {
                                public void onSuccess(Boolean b)
                                {
                                  Properties properties = new Properties();
                                  properties.put("@public", "");
                                  properties.put("@users", "");
                                 
                                  // return the default property
                                  callback.onSuccess(new ACL(file,properties,aclFile));
                                }
                                public void onFailure(Throwable t)
                                {
                                  callback.onFailure(t);
                                }
                                });
                        }
                      }
                      public void onFailure(Throwable t)
                      {
                        callback.onFailure(t);
                      }
                      });
              }
            }
            public void onFailure(Throwable t)
            {
              callback.onFailure(t);
            }});
          }
          public void onFailure(Throwable t)
          {
            callback.onFailure(t);
          }
        });
  }
 
  private final File file;
  private final Properties aclProperties;
  private final File aclFile;
 
  private ACL(File resource, Properties aclProperties, File aclFile)
  {
    this.file = resource;
    this.aclProperties =aclProperties;
    this.aclFile = aclFile;
  }
 
  // set of user ids in that ACL
  public Set<Object> getUsers()
  {
    return aclProperties.keySet();
  }
 
  // never null
  public Access[] getAccesses(String user)
  {
    Access[] accesses = null;
    String accessList = (String) aclProperties.get(user);
    if(accessList != null)
    {
      if(accessList.equals(""))
        return new Access[0];
      String fields[] = accessList.split(",");
      if(fields != null)
      {
        accesses = new Access[fields.length];
        for(int i = 0; i< fields.length ; i++)
        {
          if(fields[i].equals("" + Access.DELEGATE))
            accesses[i] = Access.DELEGATE;
          else if(fields[i].equals("" + Access.DELETE))
            accesses[i] = Access.DELETE;
          else if(fields[i].equals("" + Access.LIST))
            accesses[i] = Access.LIST;
          else if(fields[i].equals("" + Access.READ))
            accesses[i] = Access.READ;
          else if(fields[i].equals("" + Access.WRITE))
            accesses[i] = Access.WRITE;
          else if(fields[i].equals("" + Access.READ))
            accesses[i] = Access.READ;
          else
          {
            accesses[i] = null;
            Log.error("Unknown access type >" + fields[i] + "<");
          }
        }
      }
    }
   
    return accesses;
  }
 
  public boolean grantAccess( String user,Access access)
  {
    boolean result = false;
    String accesses = aclProperties.getProperty(user);
    if(accesses == null) // user is not int list
    {
      aclProperties.setProperty(user, "" + access);
      result = true;
    }
    if(accesses != null)
    {
      if( accesses.indexOf("" + access) < 0 ) // user does not have already the access
      {
        accesses += "," + access;
        aclProperties.setProperty(user, accesses);
        result = true;
      }
    }
    return result;
  }
 
  public void setAccess(String user, String accessList )
  {
    aclProperties.setProperty(user, accessList);
  }
 
  public void clear()
  {
    aclProperties.clear();
  }
  public void save(final AsyncCallback<Boolean> callback)
  {
    FileOutputStream fos = new FileOutputStream(aclFile);
    aclProperties.store(fos, "comments test", callback);
  }
 
}
TOP

Related Classes of com.zaranux.os.client.access.ACL

TOP
Copyright © 2018 www.massapi.com. 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.