This project is read-only.

Why aliasRecord is not created for a new taxonomy from Migrations

Topics: Core
Mar 15, 2015 at 9:33 PM
Edited Mar 15, 2015 at 9:36 PM
Hello I'm using Orchard 1.7.2

I create a taxonomy within a Migrations class with this code.
TaxonomyPart taxonomy = _contentManager.New<TaxonomyPart>("Taxonomy");
taxonomy.Name = "Catalogo";            
taxonomy.Slug = _slugService.Slugify("Catalogo");
_contentManager.Create(taxonomy, VersionOptions.Published);                        
Problem is Create method doesn't add a record with the slug in Orchard_Alias_AliasRecord table. So route /catalago doesn't work.
I found it can be solved editing and storing the taxonomy without changing anything through the Admin UI. The point is I need to accomplish it programmatically.

Debugging I have found the problem happens in AutoroutePartHandler when it attempts to validate user permissions within ProcessAlias method because current context when it is launched from migrations is null
        private void ProcessAlias(AutoroutePart part) {
            // generate an alias if one as not already been entered
            if (String.IsNullOrWhiteSpace(part.DisplayAlias)) {
                part.DisplayAlias = _autorouteService.Value.GenerateAlias(part);

            // should it become the home page ?
            if (part.DisplayAlias != "/" && _orchardServices.Authorizer.Authorize(Permissions.SetHomePage)) {
                // if it's the current home page, do nothing
                var currentHomePages = _orchardServices.ContentManager.Query<AutoroutePart, AutoroutePartRecord>().Where(x => x.DisplayAlias == "").List();
                if (currentHomePages.Any(x => x.Id == part.Id)) {

                var previous = part.Path;
                if (!_autorouteService.Value.ProcessPath(part))
                    _orchardServices.Notifier.Warning(T("Permalinks in conflict. \"{0}\" is already set for a previously created {2} so now it has the slug \"{1}\"",
                                                 previous, part.Path, part.ContentItem.ContentType));
Any advice of what can be done to avoid this problem?
Mar 16, 2015 at 11:47 AM
Edited Mar 16, 2015 at 12:15 PM
Most simply solution seems to modify ProcessAlias method to avoid this situation:
if (part.DisplayAlias != "/" && (_orchardServices.WorkContext==null || _orchardServices.Authorizer.Authorize(Permissions.SetHomePage))) {
But is it the right solution? I'm not sure if under a security point of view it is right. Should I open an issue and submit a pull request? Or is there another solutions for this?
Mar 19, 2015 at 11:22 PM
See in the source code of a more recent version (1.8.x, 1.x, 1.9-int). Some parts of this test are no more used
I think you can safely use a more recent code, but first compare with your current version

Mar 20, 2015 at 8:09 AM
Great thank you!
Apr 1, 2015 at 1:03 PM
Edited Apr 1, 2015 at 1:03 PM
I've migrated my site to Orchard 1.8.1 but it still doesn't work, I will migrate next week to 1.9 to check before reporting an issue