Ejemplo n.º 1
0
        /// <summary>
        /// This grantRights() method set the rights to the otheruserID
        /// </summary>
        /// <param name="otheruserID"></param>
        /// <param name="containerID"></param>
        /// <param name="writeAccess"></param>
        /// <returns>true/false</returns>
        public Boolean grantRights(int otheruserID, int containerID, Boolean writeAccess)
        {
            DBManager.ResourceM       resmgr = new DBManager.ResourceM();
            Model.AzureContainerModel rmodel = new Model.AzureContainerModel();
            Model.CListModel          cmodel = new Model.CListModel();

            try
            {
                rmodel = resmgr.getResourceById(connection, containerID);
                DBManager.CListM cmgr    = new DBManager.CListM();
                Model.CListModel cmodel1 = new Model.CListModel();
                cmodel1 = cmgr.getSharedResourceByGivenName(connection, otheruserID, rmodel.getGivenName());

                if (cmodel1 != null)
                {
                    //If permission already granted to otheruser, then check if requested permission is same
                    Boolean test = false;
                    if (cmodel1.getReadwrite() == 1)
                    {
                        test = true;
                    }
                    if (test == writeAccess)
                    {
                        return(true);    //already exist with same permission
                    }
                    else
                    {
                        cmgr.deleteUserEntryFromCLIST(connection, otheruserID, containerID); //delete the record and re-enter
                    }
                }

                cmodel.setRid(containerID);
                cmodel.setUid(otheruserID);
                cmodel.setOwner(rmodel.getOwner());
                if (writeAccess)
                {
                    cmodel.setReadwrite(1);
                }
                else
                {
                    cmodel.setReadwrite(0);
                }

                //Insert into CLIST table
                bool success = cmgr.insertIntoCList(connection, cmodel);

                return(success);
            }
            catch (DBLikeExceptions.CloudContainerNotFoundException)
            {
                return(false);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This method inserts record to the CONTAINERS table.
        /// </summary>
        /// <param name="connection"></param>
        /// <param name="uid"></param>
        /// <param name="containerName"></param>
        /// <returns>true/false</returns>
        public Boolean insertIntoResources(SqlConnection connection, Model.AzureContainerModel resource)
        {
            try
            {
                //---Should have used auto-increment
                command = new SqlCommand("SELECT MAX(RID) FROM CONTAINERS", connection);

                reader = command.ExecuteReader();
                int rid = 10000;    //starting resource id from 10001

                if (!reader.HasRows)
                {
                    rid = rid + 1;
                }
                else
                {
                    while (reader.Read())
                    {
                        rid = reader.GetInt32(0);
                    }
                    rid = rid + 1;
                }
                Console.WriteLine("Resource ID" + rid);
                command             = new SqlCommand("INSERT INTO CONTAINERS (RID, GIVENNAME, OWNER, CONTAINERNAME) VALUES (@RID, @GIVENNAME, @OWNER, @CONTAINERNAME)");
                command.CommandType = CommandType.Text;
                command.Connection  = connection;
                command.Parameters.AddWithValue("@RID", rid);
                command.Parameters.AddWithValue("@GIVENNAME", resource.getGivenName());
                command.Parameters.AddWithValue("@OWNER", resource.getOwner());
                command.Parameters.AddWithValue("@CONTAINERNAME", resource.getContainerName());
                command.ExecuteNonQuery();

                /* Following table retains the entry of user-resource which is sharing
                 * Model.CListModel cm = new Model.CListModel();
                 * cm.setRid(rid);
                 * cm.setUid(uid);
                 * cm.setReadwrite(1);
                 * reader.Close();
                 * CListM c = new CListM();
                 *
                 * //on adding resource, the default will be given to resource {DEFAULT(READ, READWRITE) = True}
                 * return c.insertIntoCList(connection, cm);
                 */
            }
            catch (Exception e)
            {
                Console.WriteLine("insertIntoResources in class User says->" + e.Message);
                return(false);
            }
            reader.Close();
            return(true);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// This isOwner() method returns true if given user is owner otherwise false
 /// FALSE: if container doesn't exit or user is not owner
 /// </summary>
 /// <param name="userID"></param>
 /// <param name="containerID"></param>
 /// <returns></returns>
 public Boolean isOwner(int userID, int containerID)
 {
     DBManager.ResourceM       resmgr = new DBManager.ResourceM();
     Model.AzureContainerModel rmodel = new Model.AzureContainerModel();
     try
     {
         rmodel = resmgr.getResourceById(connection, containerID);
         if (userID == rmodel.getOwner())
         {
             return(true);
         }
     }
     catch (DBLikeExceptions.CloudContainerNotFoundException)
     {
         return(false);
     }
     return(false);
 }
Ejemplo n.º 4
0
        /// <summary>
        /// This renameFile() method renames the file
        /// </summary>
        /// <param name="username"></param>
        /// <param name="path"></param>
        public void renameFile(String username, String path, String newname)    //path> containerID:filepath
        {
            try
            {
                //Break the path <containerID:filepath>
                String[] pathTokens = path.Split(':');

                DBManager.UserM user = new DBManager.UserM();
                Model.UserModel u;
                u = user.getUserRecord(connection, username);

                String container = pathTokens[0];
                String blobfile  = pathTokens[1];

                DBManager.ResourceM contDetail = new DBManager.ResourceM();
                Console.WriteLine("ResourceID:" + container);
                Model.AzureContainerModel cont = contDetail.getResourceById(connection, Int32.Parse(container));

                if (cont.getOwner() != u.getUid())      //user is not owner
                {
                    Resource res = new Resource();
                    if (!res.canWrite(u.getUid(), Int32.Parse(container)))       //not having write permission
                    {
                        throw new DBLikeExceptions.UnauthorizedAccessException();
                    }
                }

                container = cont.getContainerName();


                //Check if container exists
                CloudBlobContainer myContainer       = BlobStorageManager.BlobStorage.getCloudBlobContainer(container); //Container ref
                Boolean            isContainerexists = BlobStorageManager.BlobStorage.isContainerExists(myContainer);

                if (isContainerexists == false)
                {
                    Console.WriteLine("Container not found");
                    throw new DBLikeExceptions.CloudContainerNotFoundException();
                }

                //Check if blob exists
                CloudBlockBlob oldblob      = BlobStorageManager.BlobStorage.getCloudBlob(container + '/' + blobfile); //Get reference to blob
                Boolean        isBlobexists = BlobStorageManager.BlobStorage.isBlobExists(oldblob);

                if (isBlobexists == false)
                {
                    Console.WriteLine("Blob not found");
                    throw new DBLikeExceptions.CloudBlobNotFoundException();
                }

                ICloudBlob newblob = null;
                if (oldblob is CloudBlockBlob)
                {
                    newblob = myContainer.GetBlockBlobReference(newname);
                }
                else
                {
                    newblob = myContainer.GetPageBlobReference(newname);
                }

                //CloudBlockBlob newblob = BlobStorageManager.BlobStorage.getCloudBlob(container + '/' + newname); //Get reference to blob

                //copy the blob
                newblob.StartCopyFromBlob(oldblob.Uri);

                while (true)
                {
                    newblob.FetchAttributes();
                    if (newblob.CopyState.Status != CopyStatus.Pending) //check the copying status
                    {
                        break;
                    }
                    System.Threading.Thread.Sleep(1000); //sleep for a second
                }

                //delete old blobfile
                oldblob.Delete();
            }
            catch (Microsoft.WindowsAzure.Storage.StorageException e)
            {
                Console.WriteLine("File:renameFile says->>" + e.Message);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// This method takes (username, path{containerid:path}, local path) and download file from blob storage
        /// to given local path
        /// THROW UNAUTHORIZEDACCESSEXCEPTION IF USER HAS NO RIGHT TO ACCESS THE MENTIONED PATH
        /// THROW CLOUDBLOBNOTFOUNDEXCEPTION or CLOUDBLOBNOTFOUNDEXCEPTION IF GIVEN FILEPATH DOESN'T EXISTS
        /// </summary>
        /// <param name="username"></param>
        /// <param name="path"></param>
        /// <param name="data"></param>
        public void download(String username, String path, Stream targetStream) //containerid:filepath
        {
            try
            {
                //Break the path <containerid>
                String[] usercont = path.Split(':');

                DBManager.UserM user = new DBManager.UserM();
                Model.UserModel u;
                u = user.getUserRecord(connection, username);

                String containerP = usercont[0];
                String blobname   = usercont[1];

                /*
                 * Model.UserModel u2;
                 * u2 = user.getUserRecord(connection, srcUser);
                 *
                 * String userid = u2.getUid().ToString();
                 * String fullpath = usercont[1];
                 * String blobName = fullpath.Substring(fullpath.IndexOf('/')+1);
                 * //String container = fullpath.Substring(0, fullpath.IndexOf('/'));
                 */

                String container = containerP;

                DBManager.ResourceM contDetail = new DBManager.ResourceM();
                Console.WriteLine("ResourceID:" + containerP);
                Model.AzureContainerModel cont = contDetail.getResourceById(connection, Int32.Parse(containerP));

                Resource res = new Resource();
                if (cont.getOwner() != u.getUid())                         //user isn't owner
                {
                    if (!res.canRead(u.getUid(), Int32.Parse(containerP))) //not having read permission
                    {
                        throw new DBLikeExceptions.UnauthorizedAccessException();
                    }
                }
                container = cont.getContainerName();

                //Check if container exists
                CloudBlobContainer myContainer       = BlobStorageManager.BlobStorage.getCloudBlobContainer(container);
                Boolean            isContainerexists = BlobStorageManager.BlobStorage.isContainerExists(myContainer);

                if (isContainerexists == false)
                {
                    throw new DBLikeExceptions.CloudContainerNotFoundException();
                }

                //Check if blob exists
                CloudBlockBlob myblob       = BlobStorageManager.BlobStorage.getCloudBlob(container + '/' + blobname); //Get reference to blob
                Boolean        isBlobexists = BlobStorageManager.BlobStorage.isBlobExists(myblob);

                if (isBlobexists == false)
                {
                    Console.WriteLine("Container not found");
                    throw new DBLikeExceptions.CloudBlobNotFoundException();
                }
                Console.WriteLine(myblob);
                myblob.DownloadToStream(targetStream);
            }
            catch (Microsoft.WindowsAzure.Storage.StorageException ex)
            {
                //throw new Microsoft.WindowsAzure.Storage.StorageException();
                Console.WriteLine("download method in User class says->" + ex.Message);
                return;
            }
        }