public IHttpActionResult Uninstall(int packageId)
        {
            try
            {
                var pack = InstalledPackage.GetById(packageId);
                if (pack == null)
                {
                    return(NotFound());
                }

                PerformUninstall(pack);

                //now get all other packages by this name since we'll uninstall all versions
                foreach (var installed in InstalledPackage.GetAllInstalledPackages()
                         .Where(x => x.Data.Name == pack.Data.Name && x.Data.Id != pack.Data.Id))
                {
                    //remove from teh xml
                    installed.Delete(Security.GetUserId());
                }
            }
            catch (Exception e)
            {
                Logger.Error <PackageInstallController>("Failed to uninstall.", e);
                throw;
            }

            return(Ok());
        }
Exemple #2
0
        public void InstallBusinessLogic(int packageId, string tempDir)
        {
            using (Current.ProfilingLogger.DebugDuration <Installer>(
                       "Installing business logic for package id " + packageId + " into temp folder " + tempDir,
                       "Package business logic installation complete for package id " + packageId))
            {
                InstalledPackage insPack;
                try
                {
                    //retrieve the manifest to continue installation
                    insPack = InstalledPackage.GetById(packageId);
                    //bool saveNeeded = false;

                    // Get current user, with a fallback
                    var currentUser = Current.Services.UserService.GetUserById(Constants.Security.SuperUserId);

                    //TODO: Get rid of this entire class! Until then all packages will be installed by the admin user


                    //Xml as XElement which is used with the new PackagingService
                    var rootElement      = Config.DocumentElement.GetXElement();
                    var packagingService = Current.Services.PackagingService;

                    //Perhaps it would have been a good idea to put the following into methods eh?!?

                    #region DataTypes
                    var dataTypeElement = rootElement.Descendants("DataTypes").FirstOrDefault();
                    if (dataTypeElement != null)
                    {
                        var dataTypeDefinitions = packagingService.ImportDataTypeDefinitions(dataTypeElement, currentUser.Id);
                        foreach (var dataTypeDefinition in dataTypeDefinitions)
                        {
                            insPack.Data.DataTypes.Add(dataTypeDefinition.Id.ToString(CultureInfo.InvariantCulture));
                        }
                    }
                    #endregion

                    #region Languages
                    var languageItemsElement = rootElement.Descendants("Languages").FirstOrDefault();
                    if (languageItemsElement != null)
                    {
                        var insertedLanguages = packagingService.ImportLanguages(languageItemsElement);
                        insPack.Data.Languages.AddRange(insertedLanguages.Select(l => l.Id.ToString(CultureInfo.InvariantCulture)));
                    }

                    #endregion

                    #region Dictionary items
                    var dictionaryItemsElement = rootElement.Descendants("DictionaryItems").FirstOrDefault();
                    if (dictionaryItemsElement != null)
                    {
                        var insertedDictionaryItems = packagingService.ImportDictionaryItems(dictionaryItemsElement);
                        insPack.Data.DictionaryItems.AddRange(insertedDictionaryItems.Select(d => d.Id.ToString(CultureInfo.InvariantCulture)));
                    }
                    #endregion

                    #region Macros
                    var macroItemsElement = rootElement.Descendants("Macros").FirstOrDefault();
                    if (macroItemsElement != null)
                    {
                        var insertedMacros = packagingService.ImportMacros(macroItemsElement);
                        insPack.Data.Macros.AddRange(insertedMacros.Select(m => m.Id.ToString(CultureInfo.InvariantCulture)));
                    }
                    #endregion

                    #region Templates
                    var templateElement = rootElement.Descendants("Templates").FirstOrDefault();
                    if (templateElement != null)
                    {
                        var templates = packagingService.ImportTemplates(templateElement, currentUser.Id);
                        foreach (var template in templates)
                        {
                            insPack.Data.Templates.Add(template.Id.ToString(CultureInfo.InvariantCulture));
                        }
                    }
                    #endregion

                    #region DocumentTypes
                    //Check whether the root element is a doc type rather then a complete package
                    var docTypeElement = rootElement.Name.LocalName.Equals("DocumentType") ||
                                         rootElement.Name.LocalName.Equals("DocumentTypes")
                        ? rootElement
                        : rootElement.Descendants("DocumentTypes").FirstOrDefault();

                    if (docTypeElement != null)
                    {
                        var contentTypes = packagingService.ImportContentTypes(docTypeElement, currentUser.Id);
                        foreach (var contentType in contentTypes)
                        {
                            insPack.Data.Documenttypes.Add(contentType.Id.ToString(CultureInfo.InvariantCulture));
                            //saveNeeded = true;
                        }
                    }
                    #endregion

                    #region Stylesheets
                    foreach (XmlNode n in Config.DocumentElement.SelectNodes("Stylesheets/Stylesheet"))
                    {
                        //StyleSheet s = StyleSheet.Import(n, currentUser);


                        string stylesheetName = XmlHelper.GetNodeValue(n.SelectSingleNode("Name"));
                        //StyleSheet s = GetByName(stylesheetName);
                        var s = Current.Services.FileService.GetStylesheetByName(stylesheetName);
                        if (s == null)
                        {
                            s = new Stylesheet(XmlHelper.GetNodeValue(n.SelectSingleNode("FileName")))
                            {
                                Content = XmlHelper.GetNodeValue(n.SelectSingleNode("Content"))
                            };
                            Current.Services.FileService.SaveStylesheet(s);
                        }

                        foreach (XmlNode prop in n.SelectNodes("Properties/Property"))
                        {
                            string alias = XmlHelper.GetNodeValue(prop.SelectSingleNode("Alias"));
                            var    sp    = s.Properties.SingleOrDefault(p => p != null && p.Alias == alias);
                            string name  = XmlHelper.GetNodeValue(prop.SelectSingleNode("Name"));
                            if (sp == null)
                            {
                                //sp = StylesheetProperty.MakeNew(
                                //    name,
                                //    s,
                                //    u);

                                sp = new StylesheetProperty(name, "#" + name.ToSafeAlias(), "");
                                s.AddProperty(sp);
                            }
                            else
                            {
                                //sp.Text = name;
                                //Changing the name requires removing the current property and then adding another new one
                                if (sp.Name != name)
                                {
                                    s.RemoveProperty(sp.Name);
                                    var newProp = new StylesheetProperty(name, sp.Alias, sp.Value);
                                    s.AddProperty(newProp);
                                    sp = newProp;
                                }
                            }
                            sp.Alias = alias;
                            sp.Value = XmlHelper.GetNodeValue(prop.SelectSingleNode("Value"));
                        }
                        //s.saveCssToFile();
                        Current.Services.FileService.SaveStylesheet(s);



                        insPack.Data.Stylesheets.Add(s.Id.ToString(CultureInfo.InvariantCulture));
                        //saveNeeded = true;
                    }

                    //if (saveNeeded) { insPack.Save(); saveNeeded = false; }
                    #endregion

                    #region Documents
                    var documentElement = rootElement.Descendants("DocumentSet").FirstOrDefault();
                    if (documentElement != null)
                    {
                        var content          = packagingService.ImportContent(documentElement, -1, currentUser.Id);
                        var firstContentItem = content.First();
                        insPack.Data.ContentNodeId = firstContentItem.Id.ToString(CultureInfo.InvariantCulture);
                    }
                    #endregion

                    #region Package Actions
                    foreach (XmlNode n in Config.DocumentElement.SelectNodes("Actions/Action"))
                    {
                        if (n.Attributes["undo"] == null || n.Attributes["undo"].Value == "true")
                        {
                            insPack.Data.Actions += n.OuterXml;
                        }

                        //Run the actions tagged only for 'install'

                        if (n.Attributes["runat"] != null && n.Attributes["runat"].Value == "install")
                        {
                            var alias = n.Attributes["alias"] != null ? n.Attributes["alias"].Value : "";

                            if (alias.IsNullOrWhiteSpace() == false)
                            {
                                PackageAction.RunPackageAction(insPack.Data.Name, alias, n);
                            }
                        }
                    }
                    #endregion

                    insPack.Save();
                }
                catch (Exception ex)
                {
                    Current.Logger.Error <Installer>(ex, "Error installing businesslogic");
                    throw;
                }

                OnPackageBusinessLogicInstalled(insPack);
                OnPackageInstalled(insPack);
            }
        }
Exemple #3
0
        public void InstallFiles(int packageId, string tempDir)
        {
            using (Current.ProfilingLogger.DebugDuration <Installer>(
                       "Installing package files for package id " + packageId + " into temp folder " + tempDir,
                       "Package file installation complete for package id " + packageId))
            {
                //retrieve the manifest to continue installation
                var insPack = InstalledPackage.GetById(packageId);

                //TODO: Depending on some files, some files should be installed differently.
                //i.e. if stylsheets should probably be installed via business logic, media items should probably use the media IFileSystem!

                // Move files
                //string virtualBasePath = System.Web.HttpContext.Current.Request.ApplicationPath;
                string basePath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;

                try
                {
                    foreach (XmlNode n in Config.DocumentElement.SelectNodes("//file"))
                    {
                        var destPath   = GetFileName(basePath, XmlHelper.GetNodeValue(n.SelectSingleNode("orgPath")));
                        var sourceFile = GetFileName(tempDir, XmlHelper.GetNodeValue(n.SelectSingleNode("guid")));
                        var destFile   = GetFileName(destPath, XmlHelper.GetNodeValue(n.SelectSingleNode("orgName")));

                        // Create the destination directory if it doesn't exist
                        if (Directory.Exists(destPath) == false)
                        {
                            Directory.CreateDirectory(destPath);
                        }
                        //If a file with this name exists, delete it
                        else if (File.Exists(destFile))
                        {
                            File.Delete(destFile);
                        }

                        // Copy the file
                        // SJ: Note - this used to do a move but some packages included the same file to be
                        // copied to multiple locations like so:
                        //
                        // <file>
                        //   <guid>my-icon.png</guid>
                        //   <orgPath>/umbraco/Images/</orgPath>
                        //   <orgName>my-icon.png</orgName>
                        // </file>
                        // <file>
                        //   <guid>my-icon.png</guid>
                        //   <orgPath>/App_Plugins/MyPlugin/Images</orgPath>
                        //   <orgName>my-icon.png</orgName>
                        // </file>
                        //
                        // Since this file unzips as a flat list of files, moving the file the first time means
                        // that when you try to do that a second time, it would result in a FileNotFoundException
                        File.Copy(sourceFile, destFile);

                        //PPH log file install
                        insPack.Data.Files.Add(XmlHelper.GetNodeValue(n.SelectSingleNode("orgPath")) + "/" + XmlHelper.GetNodeValue(n.SelectSingleNode("orgName")));
                    }

                    // Once we're done copying, remove all the files
                    foreach (XmlNode n in Config.DocumentElement.SelectNodes("//file"))
                    {
                        var sourceFile = GetFileName(tempDir, XmlHelper.GetNodeValue(n.SelectSingleNode("guid")));
                        if (File.Exists(sourceFile))
                        {
                            File.Delete(sourceFile);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Current.Logger.Error <Installer>(ex, "Package install error");
                    throw;
                }

                // log that a user has install files
                if (_currentUserId > -1)
                {
                    Current.Services.AuditService.Add(AuditType.PackagerInstall,
                                                      _currentUserId,
                                                      -1, "Package", string.Format("Package '{0}' installed. Package guid: {1}", insPack.Data.Name, insPack.Data.PackageGuid));
                }

                insPack.Save();
            }
        }