Exemple #1
0
        public static bool CanFindAccount(this Cmdlet command, AccountIdentity account, AccountType accountType)
        {
            if (account == null)
            {
                return false;
            }
            var name = account.Name;
            var error = $"Cannot find an account with identity '{name}'.";

            if (accountType == AccountType.Role && !Role.Exists(name))
            {
                command.WriteError(new ErrorRecord(new ObjectNotFoundException(error), ErrorIds.AccountNotFound.ToString(),
                    ErrorCategory.ObjectNotFound, account));
                return false;
            }

            if (accountType == AccountType.User && !User.Exists(name))
            {
                command.WriteError(new ErrorRecord(new ObjectNotFoundException(error), ErrorIds.AccountNotFound.ToString(),
                    ErrorCategory.ObjectNotFound, account));
                return false;
            }

            return true;
        }
Exemple #2
0
        public static bool CanFindAccount(this Cmdlet command, AccountIdentity account, AccountType accountType)
        {
            var name = account.Name;
            var error = String.Format("Cannot find an account with identity '{0}'.", name);

            if (accountType == AccountType.Role && !Role.Exists(name))
            {
                command.WriteError(new ErrorRecord(new ObjectNotFoundException(error), error,
                    ErrorCategory.ObjectNotFound, account));
                return false;
            }
            if (accountType == AccountType.User && !User.Exists(name))
            {
                command.WriteError(new ErrorRecord(new ObjectNotFoundException(error), error,
                    ErrorCategory.ObjectNotFound, account));
                return false;
            }

            return true;
        }
Exemple #3
0
 public static Account GetAccountFromIdentity(this Cmdlet command, AccountIdentity identity)
 {
     Account account = identity;
     if (account == null)
     {
         var error = String.Format("Cannot find an account with identity '{0}'.", identity.Name);
         command.WriteError(new ErrorRecord(new ObjectNotFoundException(error), error,
             ErrorCategory.ObjectNotFound, identity));
     }
     return account;
 }
Exemple #4
0
        private void DeserializeUser(string userName)
        {
            var fileName = userName;

            // if path is not absolute - add the Root folder
            if (!System.IO.Path.IsPathRooted(fileName))
            {
                var identity = new AccountIdentity(userName);
                var target = string.IsNullOrEmpty(Root) || Root.EndsWith("\\") ? Root : Root + "\\";
                fileName = target + identity.Domain + @"\Roles\" + identity.Account + PathUtils.RoleExtension;
            }

            // make sure the path has the proper extension
            if (!fileName.EndsWith(PathUtils.RoleExtension, StringComparison.OrdinalIgnoreCase))
            {
                fileName += PathUtils.RoleExtension;
            }

            if (fileName.Contains("?") || fileName.Contains("*"))
            {
                var roles = System.IO.Path.GetDirectoryName(fileName);
                var domainName = System.IO.Path.GetDirectoryName(roles);
                var root = System.IO.Path.GetDirectoryName(domainName);
                foreach (var domain in Directory.EnumerateDirectories(root))
                {
                    var files = WildcardFilter(fileName, Directory.EnumerateFiles(domain + @"\Roles"), f => f).ToList();
                    foreach (var file in files)
                    {
                        DeserializeRoleFile(userName, file);
                    }
                }
            }
            else
            {
                DeserializeRoleFile(userName, fileName);
            }
        }
        public void ProcessRequest(HttpContext context)
        {
            var request = HttpContext.Current.Request;
            var userName = request.Params.Get("user");
            var password = request.Params.Get("password");
            var itemParam = request.Params.Get("script");
            var pathParam = request.Params.Get("path");
            var originParam = request.Params.Get("scriptDb");
            var apiVersion = request.Params.Get("apiVersion");
            var isUpload = request.HttpMethod.Is("POST") && request.InputStream.Length > 0;
            var unpackZip = request.Params.Get("skipunpack").IsNot("true");
            var skipExisting = request.Params.Get("skipexisting").Is("true");

            if (!CheckServiceEnabled(apiVersion, request.HttpMethod))
            {
                LogUtils.Debug($"The specified service {apiVersion} is not enabled.", this);
                return;
            }

            if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
            {
                var account = new AccountIdentity(userName);
                AuthenticationManager.Login(account.Name, password, false);
            }

            var isAuthenticated = Context.IsLoggedIn;
            var useContextDatabase = apiVersion.Is("file") || apiVersion.Is("handle") || !isAuthenticated || string.IsNullOrEmpty(originParam) || originParam.Is("current");
            var scriptDb = useContextDatabase ? Context.Database : Database.GetDatabase(originParam);
            var dbName = scriptDb.Name;

            if (!CheckServiceAuthentication(apiVersion, isAuthenticated))
            {
                LogUtils.Debug($"The specified service {apiVersion} requires authentication.", this);
                return;
            }

            if (isUpload)
            {
                switch (apiVersion)
                {
                    case "media":
                        if (ZipUtils.IsZipContent(request.InputStream) && unpackZip)
                        {
                            LogUtils.Debug("The uploaded media item will be extracted to the media library.", this);
                            using (var packageReader = new Sitecore.Zip.ZipReader(request.InputStream))
                            {
                                foreach (var zipEntry in packageReader.Entries)
                                {
                                    if (!zipEntry.IsDirectory)
                                    {
                                        ProcessMediaUpload(zipEntry.GetStream(), scriptDb, itemParam, zipEntry.Name, skipExisting);
                                    }
                                }
                            }
                        }
                        else
                        {
                            ProcessMediaUpload(request.InputStream, scriptDb, itemParam, null, skipExisting);
                        }
                        break;

                    case "file":
                        ProcessFileUpload(request.InputStream, originParam, pathParam);
                        break;
                    default:
                        LogUtils.Debug($"The specified apiVersion {apiVersion} is not supported.", this);
                        break;
                }
                return;
            }

            Item scriptItem;

            switch (apiVersion)
            {
                case "1":
                    scriptItem = scriptDb.GetItem(itemParam) ??
                                 scriptDb.GetItem(ApplicationSettings.ScriptLibraryPath + itemParam);
                    break;
                case "media":
                    ProcessMediaDownload(scriptDb, itemParam);
                    return;
                case "file":
                    ProcessFileDownload(originParam, pathParam);
                    return;
                case "handle":
                    ProcessHandle(originParam);
                    return;
                default:
                    UpdateCache(dbName);
                    if (!apiScripts.ContainsKey(dbName))
                    {
                        HttpContext.Current.Response.StatusCode = 404;
                        return;
                    }
                    var dbScripts = apiScripts[scriptDb.Name];
                    if (!dbScripts.ContainsKey(itemParam))
                    {
                        HttpContext.Current.Response.StatusCode = 404;
                        return;
                    }
                    scriptItem = scriptDb.GetItem(dbScripts[itemParam].Id);
                    apiScripts = null;
                    break;
            }

            ProcessScript(context, scriptItem);
        }