Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • SSA Backend 108 posts 640 karma points
    Sep 04, 2017 @ 14:26
    SSA Backend
    0

    Hi, all! When I developed Umbraco package, I used

    context.Services.UserService.AddSectionToAllUsers("My section").
    

    But in 7.7 AddSectionToAllUsers method is absent. So how can I add a section to all users in 7.7? Thanks.

  • Daniel Bardi 925 posts 2560 karma points
    Oct 04, 2017 @ 08:32
    Daniel Bardi
    1

    Looks like sections are based on user groups.

    foreach (var group in ApplicationContext.Current.Services.UserService.GetAllUserGroups())
    {
        group.AddAllowedSection("My section");
    }
    
  • SSA Backend 108 posts 640 karma points
    Oct 11, 2017 @ 12:20
    SSA Backend
    0

    Hi, Daniel,

    Thank you for the answer, it works, but I faced on with other problem. We develop a package which works in Umbraco versions range 7.2 - 7.7. I can't use your code (it crashes for 7.2-7.6) and also can't use my (it crashes in 7.7). So I need to build a custom solution which works with DB directly.

    Thanks, Dmytro

  • SSA Backend 108 posts 640 karma points
    Oct 11, 2017 @ 12:25
    SSA Backend
    100

    Here is a solution for thus who develops custom Umbraco packages!

    If you want to assign your section for all administrators during package setup use next code. Solution tested and worked fine in Umbraco 7.2 - 7.7 or later (of course if Umbraco guys do not rewrite users functionality again)

    public class TSectionRepo : ITSectionRepo
    {
        private readonly DatabaseSchemaHelper _dbHelper;
        private readonly DatabaseContext _dbContext;
        private readonly string _adminAlias = "admin";
    
        public TSectionRepo()
        {
            var logger = LoggerResolver.Current.Logger;
            _dbContext = ApplicationContext.Current.DatabaseContext;
            _dbHelper = new DatabaseSchemaHelper(_dbContext.Database, logger, _dbContext.SqlSyntax);
        }
    
        public void AssignTinifierToAdministrators()
        {
            // Umbraco 7.7+
            if (_dbHelper.TableExist("umbracoUserGroup2App"))
            {
                int? groupId = _dbContext.Database
                    .ExecuteScalar<int?>("select id from umbracoUserGroup where userGroupAlias = @0", _adminAlias);
                if (groupId.HasValue && groupId != 0)
                {
                    int rows = _dbContext.Database
                        .ExecuteScalar<int>("select count(*) from umbracoUserGroup2App where userGroupId = @0 and app = @1",
                            groupId.Value, PackageConstants.SectionAlias);
                    if (rows == 0)
                    {
                        _dbContext.Database.Execute("insert umbracoUserGroup2App values (@0, @1)",
                            groupId.Value, PackageConstants.SectionAlias);
                    }
    
                }
            }
            // Umbraco 7.2 - 7.6
            else if (_dbHelper.TableExist("UmbracoUser2app"))
            {
                string sql = @"select u.id from umbracoUser as u
                                inner join umbracoUserType as t on u.userType = t.id
                                    where t.userTypeAlias = @0";
                foreach (int userId in _dbContext.Database.Query<int>(sql, _adminAlias))
                {
                    int rows = _dbContext.Database
                        .ExecuteScalar<int>("select count(*) from UmbracoUser2app where [user] = @0 and app = @1",
                            userId, PackageConstants.SectionAlias);
                    if (rows == 0)
                    {
                        _dbContext.Database.Execute("insert UmbracoUser2app values (@0, @1)",
                            userId, PackageConstants.SectionAlias);
                    }
                }
    
            }
        }
    }
    

    Here is an interface

    public interface ITSectionRepo
    {
        /// <summary>
        /// assign tinifier section to all administrators
        /// </summary>
        void AssignTinifierToAdministrators();
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft