Ejemplo n.º 1
0
        public async static Task Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                var logger        = loggerFactory.CreateLogger("app");
                try
                {
                    var userManager = services.GetRequiredService <UserManager <IdentityUser> >();
                    var roleManager = services.GetRequiredService <RoleManager <IdentityRole> >();

                    await DefaultRoles.SeedAsync(userManager, roleManager);

                    await DefaultUsers.SeedBasicUserAsync(userManager, roleManager);

                    await DefaultUsers.SeedSuperAdminAsync(userManager, roleManager);

                    logger.LogInformation("Finished Seeding Default Data");
                    logger.LogInformation("Application Starting");
                }
                catch (Exception ex)
                {
                    logger.LogWarning(ex, "An error occurred seeding the DB");
                }
            }
            host.Run();
        }
Ejemplo n.º 2
0
        public async Task <IdentityResult> RemoveUserAsync(int id)
        {
            var user = await _userManager
                       .Users.FirstOrDefaultAsync(u => u.Id == id)
                       .ConfigureAwait(false);

            if (user == null)
            {
                return(IdentityResult.Failed(new IdentityError
                {
                    Code = "UserNotFound",
                    Description = "User not found!"
                }));
            }

            if (DefaultUsers.All().Select(u => u.UserName).Contains(user.UserName))
            {
                return(IdentityResult.Failed(new IdentityError()
                {
                    Code = "CannotRemoveSystemUser",
                    Description = "You cannot remove system user!"
                }));
            }

            var removeUserResult = await _userManager.DeleteAsync(user).ConfigureAwait(false);

            if (!removeUserResult.Succeeded)
            {
                return(removeUserResult);
            }

            user.UserRoles.Clear();

            return(removeUserResult);
        }
Ejemplo n.º 3
0
            public static void Apply(ModelContext context)
            {
                var existingUserIds     = context.Users.Select(u => u.Id).ToList();
                var existingCmsEntryIds = context.CmsEntries.Select(e => e.Id).ToList();

                var newUsers      = DefaultUsers.Where(u => !existingUserIds.Contains(u.Id));
                var newCmsEntries = DefaultCmsEntries.Where(e => !existingCmsEntryIds.Contains(e.Id));

                context.AddRange(newUsers);
                context.AddRange(newCmsEntries);

                context.SaveChanges();
            }
Ejemplo n.º 4
0
        public static async Task Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var context = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>();
                context.Database.Migrate();
                var roleManager = scope.ServiceProvider.GetRequiredService <RoleManager <IdentityRole> >();
                await DefaultRoles.AddDefaultRolesAsync(roleManager);

                var userManager = scope.ServiceProvider.GetRequiredService <UserManager <ApplicationUser> >();
                await DefaultUsers.AddDefaultUsers(userManager);
            }
            host.Run();
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Update(string id, ManageUserRolesViewModel manageUserRolesVM)
        {
            var user = await _userManager.FindByIdAsync(id);

            var roles = await _userManager.GetRolesAsync(user);

            var result = await _userManager.RemoveFromRolesAsync(user, roles);

            result = await _userManager.AddToRolesAsync(user, manageUserRolesVM.UserRoles.Where( x => x.Selected).Select(y => y.RoleName));

            //reapply roles and permissions for superAdmin in case any other admin tries to change its permissions
            //this can be changed or deleted depending on the business logic
            var currentUser = await _userManager.GetUserAsync(User);

            await _singInManager.RefreshSignInAsync(currentUser);

            await DefaultUsers.SeedSuperAdminAsync(_userManager, _roleManager);

            return(RedirectToAction(nameof(Index), new { userId = id }));
        }
Ejemplo n.º 6
0
        public static async Task Main(string[] args)
        {
            var    host = CreateHostBuilder(args).Build();
            string connectionString;

            using (var scope = host.Services.CreateScope())
            {
                IConfiguration configuration = scope.ServiceProvider.GetRequiredService <IConfiguration>();
                connectionString = configuration.GetConnectionString("Default");
            }
            Migration.ConnectionString = connectionString;
            await Migration.MigrateAsync();

            DefaultUsers.ConnectionString = connectionString;
            await DefaultUsers.AddDefaultUsers();

            DefaultAuthors.ConnectionString = connectionString;
            await DefaultAuthors.AddDefaultAuthors();

            DefaultCategories.ConnectionString = connectionString;
            await DefaultCategories.AddDefaultCategories();

            await host.RunAsync();
        }
Ejemplo n.º 7
0
 public static User[] BuildApplicationUsers()
 {
     return(DefaultUsers.All().ToArray());
 }
Ejemplo n.º 8
0
        public static void GetUserTabel(Literal userlist)
        {
            var    defaultRols  = DefaultRols.GetDefaultRols();
            var    defaultUsers = DefaultUsers.GetDefaultUsers();
            string warning;
            string info;
            var    usertabel = UserTabel(200, out warning, out info);

            userlist.Text = "<Table class=\"table table-header-rotated\">";
            var linecount = 0;

            foreach (var tabelrow in usertabel)
            {
                userlist.Text += "<tr>";
                var rowcount = 0;
                foreach (var tabelfield in tabelrow)
                {
                    if (rowcount == 1)
                    {
                        //do nothings, skip comment, use this as optional title in the User column
                    }
                    else if (linecount == 0 && rowcount >= 9)
                    {
                        var style = "";
                        var note  = "";
                        if (tabelfield.EndsWith("?EVERYONE?"))
                        {
                            note = " *e";
                        }
                        var rolfield   = tabelfield.Replace("?EVERYONE?", "");
                        var defaultRol = defaultRols.SingleOrDefault(x => x.Rol == rolfield);
                        if (defaultRol != null)
                        {
                            defaultRol.Hit = true;
                            style          = " style=\"color:#008800;\"";
                        }
                        string comment;
                        DefaultRols.RolComment.TryGetValue(rolfield, out comment);
                        userlist.Text += string.Format("<th class=\"rotate\"><div><span><a href=\"?account={0}&t=d\"{1} title=\"{2}\">{0}</a>{3}</span></div></th>", rolfield, style, comment, note);
                    }
                    else if (linecount == 0)
                    {
                        userlist.Text += "<th class=\"norotate\" valign=\"bottom\"><div><span>" + tabelfield + "</span></div></th>";
                    }
                    else if (rowcount == 0)
                    {
                        var style       = "";
                        var defaultUser = defaultUsers.SingleOrDefault(x => x.User == tabelfield);
                        if (defaultUser != null)
                        {
                            defaultUser.Hit = true;
                            style           = " style=\"color:#008800;\"";
                        }
                        string comment;
                        if (!DefaultUsers.UserComment.TryGetValue(tabelfield, out comment))
                        {
                            if (tabelrow.Count > 1)
                            {
                                comment = tabelrow[1];
                            }
                        }
                        userlist.Text += string.Format("<td nowrap><a href=\"?account={0}&t=u\"{1} title=\"{2}\">{0}</a></td>", tabelfield, style, comment);
                    }
                    else if (rowcount >= 9)
                    {
                        userlist.Text += "<td nowrap class=\"xfield\">" + tabelfield + "</td>";
                    }
                    else
                    {
                        userlist.Text += "<td nowrap>" + tabelfield + "</td>";
                    }
                    rowcount++;
                }
                userlist.Text += "</tr>";
                linecount++;
            }
            userlist.Text += "<tr><td><a href=\"?account=$currentuser&t=u\" title=\"replace token for Standard value\">$currentuser</a></td><td style=\"text-align: left\" colspan=\"25\" align=\"left\">Not a real user but a Sitecore replace token, can be used for item rights on __Standard Values or Branches, but no roles</td></tr>";
            userlist.Text += "</Table><p>With this tool you can view a all users and roles. It can be used to do audits. You can see which users and roles are custom or default Sitecore, and get reported as default Sitecore users or roles missing.<br>" + info + "</p>";
            if (!string.IsNullOrEmpty(warning))
            {
                userlist.Text += "<br><span style=\"color:#880000;\">WARNING: " + warning;
            }
            var warningRols = defaultRols.Where(x => x.Hit == false).ToList();

            if (warningRols.Any())
            {
                userlist.Text += "<br><span style=\"color:#880000;\">WARNING:</span> Expected roles not found: ";
                foreach (var warningRol in warningRols)
                {
                    userlist.Text += warningRol.Rol + " ";
                }
            }
            var warningUsers = defaultUsers.Where(x => x.Hit == false).ToList();

            if (warningUsers.Any())
            {
                userlist.Text += "<br><span style=\"color:#880000;\">WARNING:</span> Expected user not found: ";
                foreach (var warningUser in warningUsers)
                {
                    userlist.Text += warningUser.User + " ";
                }
            }

            userlist.Text += "<p><a href=\"?account=all\">Show all Right</a><br><a href=\"/sitecore modules/Shell/Security-Rights-Reporting/Download.aspx\">Download</a><br/><a href=\"?\">Simple Account list</a><br>";
            userlist.Text += "<a href=\"?rolesexport=step1\">Export Roles and Right</a><br><a href=\"?rolesexport=import1\">Import Roles and Right</a></p>";
            userlist.Text += "<p><strong>Legenda:</strong><br>X Role is assigned to user<br>* isAdmin<br>*e Everyone role (can only assign to a item, All users are assigned to the Everyone role)<br><span style=\"color:#008800;\">Green Role / User</span> is expected in Your Sitecore version:" + Sitecore.Configuration.About.Version + "</p>";
        }