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.
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();
}
Umbraco 7.7 UserService
Hi, all! When I developed Umbraco package, I used
But in 7.7 AddSectionToAllUsers method is absent. So how can I add a section to all users in 7.7? Thanks.
Looks like sections are based on user groups.
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
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)
Here is an interface
is working on a reply...