Ejemplo n.º 1
0
        /// <summary>
        /// Checks whether a user has the provided permissions on the provided content.
        /// </summary>
        /// <param name="contentId">Id of a content.</param>
        /// <param name="permissions">Permission names to check.</param>
        /// <param name="user">The user who's permissions need to be checked. If it is not provided, the server checks the current user.</param>
        /// <param name="server">Target server.</param>
        public static async Task <bool> HasPermissionAsync(int contentId, string[] permissions, string user = null, ServerContext server = null)
        {
            if (permissions == null || permissions.Length == 0)
            {
                throw new InvalidOperationException("Please provide at least one permission entry.");
            }

            var requestData = new ODataRequest()
            {
                SiteUrl    = ServerContext.GetUrl(server),
                ContentId  = contentId,
                ActionName = "HasPermission"
            };

            requestData.Parameters.Add("permissions", string.Join(",", permissions));

            if (!string.IsNullOrEmpty(user))
            {
                requestData.Parameters.Add("user", user);
            }

            var result = await RESTCaller.GetResponseStringAsync(requestData.GetUri(), server);

            bool hasPermission;

            if (bool.TryParse(result, out hasPermission))
            {
                return(hasPermission);
            }

            return(false);
        }
        /// <summary>
        /// Checks whether a user has the provided permissions on the provided content.
        /// </summary>
        /// <param name="contentId">Id of a content.</param>
        /// <param name="permissions">Permission names to check.</param>
        /// <param name="user">The user who's permissions need to be checked. If it is not provided, the server checks the current user.</param>
        /// <param name="server">Target server.</param>
        public static async Task <bool> HasPermissionAsync(int contentId, string[] permissions, string user = null, ServerContext server = null)
        {
            if (permissions == null || permissions.Length == 0)
            {
                throw new InvalidOperationException("Please provide at least one permission entry.");
            }

            var requestData = new ODataRequest(server)
            {
                ContentId   = contentId,
                ActionName  = "HasPermission",
                Permissions = permissions,
                User        = user
            };

            var result = await RESTCaller.GetResponseStringAsync(requestData.GetUri(), server).ConfigureAwait(false);

            bool hasPermission;

            if (bool.TryParse(result, out hasPermission))
            {
                return(hasPermission);
            }

            return(false);
        }
Ejemplo n.º 3
0
        // ============================================================================== Static methods

        private static async Task <SyncConfiguration> LoadConfiguration()
        {
            try
            {
                dynamic settingsContent = Content.LoadAsync(SettingsPath).Result;
                if (settingsContent == null)
                {
                    return(null);
                }

                string binaryUrl = _siteUrl.TrimEnd('/') + settingsContent.Binary.__mediaresource.media_src +
                                   "&includepasswords=true";

                var settingsText = await RESTCaller.GetResponseStringAsync(new Uri(binaryUrl));

                var config = JsonHelper.Deserialize <SyncConfiguration>(settingsText);

                // decrypt passwords and inject them back to the configuration
                foreach (var server in config.Servers.Where(server => server.LogonCredentials != null && !string.IsNullOrEmpty(server.LogonCredentials.Password)))
                {
                    var request = new ODataRequest
                    {
                        ActionName          = "Decrypt",
                        Path                = "Root",
                        IsCollectionRequest = false,
                        SiteUrl             = _siteUrl
                    };

                    try
                    {
                        server.LogonCredentials.Password = await RESTCaller.GetResponseStringAsync(
                            request.GetUri(),
                            ClientContext.Current.Servers[0],
                            HttpMethod.Post,
                            JsonHelper.Serialize(new { text = server.LogonCredentials.Password }));
                    }
                    catch (ClientException cex)
                    {
                        AdLog.LogError("Error during password decryption. " + Common.FormatClientException(cex));
                    }
                    catch (Exception ex)
                    {
                        AdLog.LogException(ex);
                    }
                }

                // preload all AD-related content types from the server
                ADRelatedContentTypes = await LoadADRelatedContentTypes();

                return(config);
            }
            catch (Exception ex)
            {
                AdLog.LogException(ex);
            }

            return(null);
        }