protected List <ESPermission> GetGridViewPermissions(SelectedState state)
        {
            List <ESPermission> permissions = new List <ESPermission>();

            for (int i = 0; i < gvPermissions.Rows.Count; i++)
            {
                GridViewRow row       = gvPermissions.Rows[i];
                CheckBox    chkSelect = (CheckBox)row.FindControl("chkSelect");
                if (chkSelect == null)
                {
                    continue;
                }

                ESPermission permission = new ESPermission();
                permission.Account     = (string)gvPermissions.DataKeys[i][0];
                permission.Access      = ((Literal)row.FindControl("litAccess")).Text;
                permission.DisplayName = ((Literal)row.FindControl("litAccount")).Text;

                if (state == SelectedState.All ||
                    (state == SelectedState.Selected && chkSelect.Checked) ||
                    (state == SelectedState.Unselected && !chkSelect.Checked))
                {
                    permissions.Add(permission);
                }
            }

            return(permissions);
        }
 public static void SetESFolderPermissionSettings(int itemId, SystemFile folder, ESPermission[] permissions)
 {
     SetESFolderPermissionSettingsInternal("SET_ENTERPRISE_FOLDER_GENERAL_SETTINGS", itemId, folder, permissions);
 }
 /// <remarks/>
 public System.IAsyncResult BeginSetEnterpriseFolderSettings(int itemId, SystemFile folder, ESPermission[] permissions, bool directoyBrowsingEnabled, int quota, QuotaType quotaType, System.AsyncCallback callback, object asyncState) {
     return this.BeginInvoke("SetEnterpriseFolderSettings", new object[] {
                 itemId,
                 folder,
                 permissions,
                 directoyBrowsingEnabled,
                 quota,
                 quotaType}, callback, asyncState);
 }
        public void SetPermissions(ESPermission[] permissions)
		{
			BindAccounts(permissions, false);
		}
        protected void BindAccounts(ESPermission[] newPermissions, bool preserveExisting)
		{
			// get binded addresses
            List<ESPermission> permissions = new List<ESPermission>();
			if(preserveExisting)
                permissions.AddRange(GetGridViewPermissions(SelectedState.All));

			// add new accounts
            if (newPermissions != null)
			{
                foreach (ESPermission newPermission in newPermissions)
				{
					// check if exists
					bool exists = false;
                    foreach (ESPermission permission in permissions)
					{
						if (String.Compare(newPermission.Account, permission.Account, true) == 0)
						{
							exists = true;
							break;
						}
					}

					if (exists)
						continue;

                    permissions.Add(newPermission);
				}
			}

            gvPermissions.DataSource = permissions;
            gvPermissions.DataBind();
		}
 public void SetEnterpriseFolderSettings(int itemId, SystemFile folder, ESPermission[] permissions, bool directoyBrowsingEnabled, int quota, QuotaType quotaType)
 {
     EnterpriseStorageController.StartSetEnterpriseFolderSettingsBackgroundTask(itemId, folder, permissions, directoyBrowsingEnabled, quota, quotaType);
 }
        protected static void StartESBackgroundTaskInternal(string taskName, int itemId, SystemFile folder, ESPermission[] permissions, bool directoyBrowsingEnabled, int quota, QuotaType quotaType)
        {
            // load organization
            var org = OrganizationController.GetOrganization(itemId);

            new Thread(() =>
            {
                try
                {
                    TaskManager.StartTask("ENTERPRISE_STORAGE", taskName, org.PackageId);

                    EnterpriseStorageController.SetFRSMQuotaOnFolder(itemId, folder.Name, quota, quotaType);
                    EnterpriseStorageController.SetFolderPermission(itemId, folder.Name, permissions);
                }
                catch (Exception ex)
                {
                    // log error
                    TaskManager.WriteError(ex, "Error executing Cloud Folders background task");
                }
                finally
                {
                    // complete task
                    try
                    {
                        TaskManager.CompleteTask();
                    }
                    catch (Exception)
                    {
                    }
                }

            }).Start();
        }
        private static ResultObject SetDriveMapsTargetingFilter(Organization org, ESPermission[] permissions, string folderName)
        {
            ResultObject result = TaskManager.StartResultTask<ResultObject>("ENTERPRISE_STORAGE", "SET_MAPPED_DRIVE_TARGETING_FILTER");

            try
            {
                Organizations orgProxy = OrganizationController.GetOrganizationProxy(org.ServiceId);

                List<ExchangeAccount> accounts = new List<ExchangeAccount>();

                foreach (var permission in permissions)
                {
                    accounts.Add(ObjectUtils.FillObjectFromDataReader<ExchangeAccount>(DataProvider.GetExchangeAccountByAccountName(org.Id, permission.Account)));
                }

                orgProxy.SetDriveMapsTargetingFilter(org.OrganizationId, accounts.ToArray(), folderName);
            }
            catch (Exception ex)
            {
                result.AddError("ENTERPRISE_STORAGE_SET_MAPPED_DRIVE_TARGETING_FILTER", ex);
            }
            finally
            {
                if (!result.IsSuccess)
                {
                    TaskManager.CompleteResultTask(result);
                }
                else
                {
                    TaskManager.CompleteResultTask();
                }
            }

            return result;
        }
 /// <remarks/>
 public System.IAsyncResult BeginSetEnterpriseFolderPermissions(int itemId, string folderName, ESPermission[] permission, System.AsyncCallback callback, object asyncState) {
     return this.BeginInvoke("SetEnterpriseFolderPermissions", new object[] {
                 itemId,
                 folderName,
                 permission}, callback, asyncState);
 }
 public ResultObject SetEnterpriseFolderPermissions(int itemId, string folderName, ESPermission[] permission) {
     object[] results = this.Invoke("SetEnterpriseFolderPermissions", new object[] {
                 itemId,
                 folderName,
                 permission});
     return ((ResultObject)(results[0]));
 }
 /// <remarks/>
 public void SetEnterpriseFolderPermissionSettingsAsync(int itemId, SystemFile folder, ESPermission[] permissions) {
     this.SetEnterpriseFolderPermissionSettingsAsync(itemId, folder, permissions, null);
 }
 /// <remarks/>
 public System.IAsyncResult BeginSetEnterpriseFolderPermissionSettings(int itemId, SystemFile folder, ESPermission[] permissions, System.AsyncCallback callback, object asyncState) {
     return this.BeginInvoke("SetEnterpriseFolderPermissionSettings", new object[] {
                 itemId,
                 folder,
                 permissions}, callback, asyncState);
 }
 public void SetEnterpriseFolderPermissionSettings(int itemId, SystemFile folder, ESPermission[] permissions) {
     this.Invoke("SetEnterpriseFolderPermissionSettings", new object[] {
                 itemId,
                 folder,
                 permissions});
 }
 /// <remarks/>
 public void SetEnterpriseFolderSettingsAsync(int itemId, SystemFile folder, ESPermission[] permissions, bool directoyBrowsingEnabled, int quota, QuotaType quotaType, object userState) {
     if ((this.SetEnterpriseFolderSettingsOperationCompleted == null)) {
         this.SetEnterpriseFolderSettingsOperationCompleted = new System.Threading.SendOrPostCallback(this.OnSetEnterpriseFolderSettingsOperationCompleted);
     }
     this.InvokeAsync("SetEnterpriseFolderSettings", new object[] {
                 itemId,
                 folder,
                 permissions,
                 directoyBrowsingEnabled,
                 quota,
                 quotaType}, this.SetEnterpriseFolderSettingsOperationCompleted, userState);
 }
 /// <remarks/>
 public void SetEnterpriseFolderSettingsAsync(int itemId, SystemFile folder, ESPermission[] permissions, bool directoyBrowsingEnabled, int quota, QuotaType quotaType) {
     this.SetEnterpriseFolderSettingsAsync(itemId, folder, permissions, directoyBrowsingEnabled, quota, quotaType, null);
 }
        private static ESPermission[] ConvertToESPermission(int itemId, WebDavFolderRule[] rules)
        {
            var permissions = new List<ESPermission>();

            foreach (var rule in rules)
            {
                var permission = new ESPermission();

                permission.Account = rule.Users.Any() ? rule.Users[0] : rule.Roles[0];

                permission.IsGroup = rule.Roles.Any();

                var orgObj = OrganizationController.GetAccountByAccountName(itemId, permission.Account);

                if (orgObj == null)
                    continue;

                if (permission.IsGroup)
                {
                    var secGroupObj = OrganizationController.GetSecurityGroupGeneralSettings(itemId, orgObj.AccountId);

                    if (secGroupObj == null)
                        continue;

                    permission.DisplayName = secGroupObj.DisplayName;

                }
                else
                {
                    var userObj = OrganizationController.GetUserGeneralSettings(itemId, orgObj.AccountId);

                    if (userObj == null)
                        continue;

                    permission.DisplayName = userObj.DisplayName;
                }

                if (rule.Read && !rule.Write)
                {
                    permission.Access = "Read-Only";
                }
                if (rule.Write)
                {
                    permission.Access = "Read-Write";
                }

                permissions.Add(permission);
            }

            return permissions.ToArray();

        }
        private static UserPermission[] ConvertToUserPermissions(int itemId, ESPermission[] permissions)
        {
            var rules = ConvertToWebDavRule(itemId, permissions);

            return ConvertToUserPermissions(rules);
        }
 /// <remarks/>
 public void SetEnterpriseFolderPermissionsAsync(int itemId, string folderName, ESPermission[] permission) {
     this.SetEnterpriseFolderPermissionsAsync(itemId, folderName, permission, null);
 }
        protected static void SetESFolderPermissionSettingsInternal(string taskName, int itemId, SystemFile folder, ESPermission[] permissions)
        {
            // load organization
            var org = OrganizationController.GetOrganization(itemId);

            new Thread(() =>
            {
                try
                {
                    TaskManager.StartTask("ENTERPRISE_STORAGE", taskName, org.PackageId);

                    EnterpriseStorageController.SetFolderPermission(itemId, folder.Name, permissions);


                    var esFolder = ObjectUtils.FillObjectFromDataReader<EsFolder>(DataProvider.GetEnterpriseFolder(itemId, folder.Name));

                    if (esFolder.StorageSpaceFolderId != null)
                    {
                        StorageSpacesController.SetFolderNtfsPermissions(esFolder.StorageSpaceId, esFolder.Path, ConvertToUserPermissions(itemId, permissions.ToArray()), true, false);
                    }
                }
                catch (Exception ex)
                {
                    // log error
                    TaskManager.WriteError(ex, "Error executing Cloud Folders background task");
                }
                finally
                {
                    // complete task
                    try
                    {
                        TaskManager.CompleteTask();
                    }
                    catch (Exception)
                    {
                    }
                }

            }).Start();
        }
 /// <remarks/>
 public void SetEnterpriseFolderPermissionsAsync(int itemId, string folderName, ESPermission[] permission, object userState) {
     if ((this.SetEnterpriseFolderPermissionsOperationCompleted == null)) {
         this.SetEnterpriseFolderPermissionsOperationCompleted = new System.Threading.SendOrPostCallback(this.OnSetEnterpriseFolderPermissionsOperationCompleted);
     }
     this.InvokeAsync("SetEnterpriseFolderPermissions", new object[] {
                 itemId,
                 folderName,
                 permission}, this.SetEnterpriseFolderPermissionsOperationCompleted, userState);
 }
 public ResultObject SetEnterpriseFolderPermissions(int itemId, string folderName, ESPermission[] permission)
 {
     return EnterpriseStorageController.SetFolderPermission(itemId, folderName, permission);
 }
 public static ResultObject SetFolderPermission(int itemId, string folder, ESPermission[] permission)
 {
     return SetFolderWebDavRulesInternal(itemId, folder, permission);
 }
 public void SetEnterpriseFolderPermissionSettings(int itemId, SystemFile folder, ESPermission[] permissions)
 {
     EnterpriseStorageController.SetESFolderPermissionSettings(itemId, folder, permissions);
 }
        protected static ResultObject SetFolderWebDavRulesInternal(int itemId, string folder, ESPermission[] permission)
        {
            ResultObject result = TaskManager.StartResultTask<ResultObject>("ENTERPRISE_STORAGE", "SET_WEBDAV_FOLDER_RULES");

            try
            {
                // load organization
                Organization org = OrganizationController.GetOrganization(itemId);
                if (org == null)
                {
                    return null;
                }

                var rules = ConvertToWebDavRule(itemId, permission);

                EnterpriseStorage es = GetEnterpriseStorage(GetEnterpriseStorageServiceID(org.PackageId));

                var webDavSetting = ObjectUtils.FillObjectFromDataReader<WebDavSetting>(
                    DataProvider.GetEnterpriseFolder(itemId, folder));

                es.SetFolderWebDavRules(org.OrganizationId, folder, webDavSetting, rules);

                var path = GetDriveMapPath(itemId, org.OrganizationId, folder);

                EnterpriseStorageController.SetDriveMapsTargetingFilter(org, permission, path);
            }
            catch (Exception ex)
            {
                result.AddError("ENTERPRISE_STORAGE_SET_WEBDAV_FOLDER_RULES", ex);
            }
            finally
            {
                if (!result.IsSuccess)
                {
                    TaskManager.CompleteResultTask(result);
                }
                else
                {
                    TaskManager.CompleteResultTask();
                }
            }

            return result;
        }
        protected List<ESPermission> GetGridViewPermissions(SelectedState state)
        {
            List<ESPermission> permissions = new List<ESPermission>();
            for (int i = 0; i < gvPermissions.Rows.Count; i++)
            {
                GridViewRow row = gvPermissions.Rows[i];
                CheckBox chkSelect = (CheckBox)row.FindControl("chkSelect");
                if (chkSelect == null)
                    continue;

                ESPermission permission = new ESPermission();
                permission.Account = (string)gvPermissions.DataKeys[i][0];
                permission.Access = ((Literal)row.FindControl("litAccess")).Text;
                permission.DisplayName = ((Literal)row.FindControl("litAccount")).Text;

                if (state == SelectedState.All ||
                    (state == SelectedState.Selected && chkSelect.Checked) ||
                    (state == SelectedState.Unselected && !chkSelect.Checked))
                    permissions.Add(permission);
            }
            
            return permissions;
        }
 public static void StartSetEnterpriseFolderSettingsBackgroundTask(int itemId, SystemFile folder, ESPermission[] permissions, bool directoyBrowsingEnabled, int quota, QuotaType quotaType)
 {
     StartESBackgroundTaskInternal("SET_ENTERPRISE_FOLDER_SETTINGS", itemId, folder, permissions, directoyBrowsingEnabled, quota, quotaType);
 }
        private static WebDavFolderRule[] ConvertToWebDavRule(int itemId, ESPermission[] permissions)
        {
            var rules = new List<WebDavFolderRule>();

            foreach (var permission in permissions)
            {
                var rule = new WebDavFolderRule();

                var account = ObjectUtils.FillObjectFromDataReader<ExchangeAccount>(DataProvider.GetExchangeAccountByAccountName(itemId, permission.Account));

                if (account.AccountType == ExchangeAccountType.SecurityGroup
                    || account.AccountType == ExchangeAccountType.DefaultSecurityGroup)
                {
                    rule.Roles.Add(permission.Account);
                    permission.IsGroup = true;
                }
                else
                {
                    rule.Users.Add(permission.Account);
                }

                if (permission.Access.ToLower().Contains("read-only"))
                {
                    rule.Read = true;
                }

                if (permission.Access.ToLower().Contains("read-write"))
                {
                    rule.Write = true;
                    rule.Read = true;
                    rule.Source = true;
                }

                rule.Source = true;

                rule.Pathes.Add("*");

                rules.Add(rule);
            }

            return rules.ToArray();
        }
 public void SetEnterpriseFolderSettings(int itemId, SystemFile folder, ESPermission[] permissions, bool directoyBrowsingEnabled, int quota, QuotaType quotaType) {
     this.Invoke("SetEnterpriseFolderSettings", new object[] {
                 itemId,
                 folder,
                 permissions,
                 directoyBrowsingEnabled,
                 quota,
                 quotaType});
 }