예제 #1
0
 public UserAdministrationController(
     TicketDeskUserManager userManager,
     TicketDeskRoleManager roleManager,
     TdDomainContext domainContext)
 {
     UserManager   = userManager;
     RoleManager   = roleManager;
     DomainContext = domainContext;
 }
예제 #2
0
        public static void SetupDemoIdentityData(TdIdentityContext context, string currentUserId)
        {
            var userStore   = new UserStore <TicketDeskUser>(context);
            var roleStore   = new RoleStore <TicketDeskRole>(context);
            var userManager = new TicketDeskUserManager(userStore);
            var roleManager = new TicketDeskRoleManager(roleStore);

            roleManager.EnsureDefaultRolesExist();


            var staff = new TicketDeskUser {
                Id = "72bdddfb-805a-4883-94b9-aa494f5f52dc", UserName = "******", Email = "*****@*****.**", DisplayName = "HelpDesk User"
            };
            var reguser = new TicketDeskUser {
                Id = "17f78f38-fa68-445f-90de-38896140db28", UserName = "******", Email = "*****@*****.**", DisplayName = "Regular User"
            };
            var users = new List <TicketDeskUser> {
                staff, reguser
            };
            var rolesNames = new Dictionary <string, string[]>
            {
                { "*****@*****.**", new[] { "TdHelpDeskUsers" } },
                { "*****@*****.**", new[] { "TdInternalUsers" } }
            };

            if (currentUserId == null && userManager.FindByName("*****@*****.**") == null)
            {
                users.Add(new TicketDeskUser
                {
                    Id          = "64165817-9cb5-472f-8bfb-6a35ca54be6a",
                    UserName    = "******",
                    Email       = "*****@*****.**",
                    DisplayName = "Admin User"
                });
                rolesNames.Add("*****@*****.**", new[] { "TdAdministrators" });
            }


            foreach (var tdUser in users)
            {
                var user = userManager.FindById(tdUser.Id);
                if (user != null)
                {
                    userManager.Delete(user);
                }
                user = tdUser;
                userManager.Create(user, "123456");

                var rnames       = rolesNames[user.UserName];
                var rolesForUser = userManager.GetRoles(user.Id);
                foreach (var rname in rnames.Where(rname => !rolesForUser.Contains(rname)))
                {
                    userManager.AddToRole(user.Id, rname);
                }
            }
        }
예제 #3
0
        public static void InitializeStockRoles(TdIdentityContext context)
        {
            //create the standard roles and default admin account
            //var userStore = new UserStore<TicketDeskUser>(context);
            var roleStore = new RoleStore <TicketDeskRole>(context);

            //var userManager = new TicketDeskUserManager(userStore);
            var roleManager = new TicketDeskRoleManager(roleStore);

            roleManager.EnsureDefaultRolesExist();
        }
예제 #4
0
        /// <summary>
        /// Migrates the users and roles from a legacy database to the new TD 2.5 schema.
        /// </summary>
        /// <param name="context">The identity database context</param>
        /// <param name="userManager">The user manager.</param>
        /// <param name="roleManager">The role manager.</param>
        /// <returns><c>true</c> if users migrated, <c>false</c> otherwise.</returns>
        public static bool MigrateSecurity(TdIdentityContext context, TicketDeskUserManager userManager, TicketDeskRoleManager roleManager)
        {
            roleManager.EnsureDefaultRolesExist();
            var appId =
                context.Database.SqlQuery<Guid>(
                    "select ApplicationId from aspnet_Applications where ApplicationName = 'TicketDesk'").First().ToString();
            var users = context.Database.SqlQuery<LegacyUser>(
                "select UserId, Email, Password, PasswordFormat, Comment from aspnet_Membership where ApplicationId = '" + appId + "' and IsApproved = 1 and IsLockedOut = 0").ToList();
            const string roleQuery = "SELECT r.RoleName FROM aspnet_UsersInRoles u inner join aspnet_Roles r on u.RoleId = r.RoleId WHERE u.UserId = @userId and r.ApplicationId = @appId";

            foreach (var user in users)
            {
                var newUser = new TicketDeskUser
                {
                    UserName = user.Email,
                    Email = user.Email,
                    DisplayName = user.Comment,
                };

                var result = user.PasswordFormat == 0 ?
                    userManager.Create(newUser, user.Password) :
                    userManager.Create(newUser);

                if (result.Succeeded)
                {
                    var rolesForUser =
                        context.Database.SqlQuery<string>(roleQuery,
                        new SqlParameter("userId", user.UserId),
                        new SqlParameter("appId", appId));
                    var newRoles = new List<string>();
                    foreach (var role in rolesForUser)
                    {
                        switch (role.ToLowerInvariant())
                        {
                            case "administrators":
                                newRoles.Add("TdAdministrators");
                                break;
                            case "helpdesk":
                                newRoles.Add("TdHelpDeskUsers");
                                break;
                            case "ticketsubmitters":
                                newRoles.Add("TdInternalUsers");
                                break;
                            default:
                                newRoles.Add("TdPendingUsers");
                                break;
                        }
                    }
                    userManager.AddToRoles(newUser.Id, newRoles.ToArray());
                }
            }
            return true;
        }
예제 #5
0
        public static void InitializeStockUsers(TdIdentityContext context)
        {
            //create the standard roles and default admin account
            var userStore = new UserStore <TicketDeskUser>(context);
            var roleStore = new RoleStore <TicketDeskRole>(context);

            var userManager = new TicketDeskUserManager(userStore);
            var roleManager = new TicketDeskRoleManager(roleStore);

            roleManager.EnsureDefaultRolesExist();


            var existingAdminRole = roleManager.FindByName("TdAdministrators");

            //only create default admin user if no other user exists with the admin role
            if (existingAdminRole != null &&
                !userManager.Users.Any(u => u.Roles.Any(r => r.RoleId == existingAdminRole.Id)))
            {
                var admin = new TicketDeskUser
                {
                    Id          = "64165817-9cb5-472f-8bfb-6a35ca54be6a",
                    UserName    = "******",
                    Email       = "*****@*****.**",
                    DisplayName = "Admin User",
                };
                if (userManager.FindById("64165817-9cb5-472f-8bfb-6a35ca54be6a") == null)
                {
                    var adminRoles = new[] { "TdAdministrators" };
                    userManager.Create(admin, "123456");

                    foreach (var rname in adminRoles)
                    {
                        userManager.AddToRole(admin.Id, rname);
                    }
                }
            }
        }
예제 #6
0
        public static void SetupDemoIdentityData(TdIdentityContext context)
        {
            var userStore = new UserStore<TicketDeskUser>(context);
            var roleStore = new RoleStore<TicketDeskRole>(context);
            var userManager = new TicketDeskUserManager(userStore);
            var roleManager = new TicketDeskRoleManager(roleStore);

            roleManager.EnsureDefaultRolesExist();

            var admin = new TicketDeskUser { Id = "64165817-9cb5-472f-8bfb-6a35ca54be6a", UserName = "******", Email = "*****@*****.**", DisplayName = "Admin User" };
            var staff = new TicketDeskUser { Id = "72bdddfb-805a-4883-94b9-aa494f5f52dc", UserName = "******", Email = "*****@*****.**", DisplayName = "HelpDesk User" };
            var reguser = new TicketDeskUser { Id = "17f78f38-fa68-445f-90de-38896140db28", UserName = "******", Email = "*****@*****.**", DisplayName = "Regular User" };
            var users = new[] { admin, staff, reguser };
            var rolesNames = new Dictionary<string, string[]>
            {
                {"*****@*****.**", new[] {"TdAdministrators"}},
                {"*****@*****.**", new[] {"TdHelpDeskUsers"}},
                {"*****@*****.**", new[] {"TdInternalUsers"}}
            };
            foreach (var tdUser in users)
            {

                var user = userManager.FindByName(tdUser.UserName);
                if (user == null)
                {
                    user = tdUser;
                    userManager.Create(user, "123456");
                }
                var rnames = rolesNames[user.UserName];
                var rolesForUser = userManager.GetRoles(user.Id);
                foreach (var rname in rnames.Where(rname => !rolesForUser.Contains(rname)))
                {
                    userManager.AddToRole(user.Id, rname);
                }
            }
        }
        /// <summary>
        /// Migrates the users and roles from a legacy database to the new TD 2.5 schema.
        /// </summary>
        /// <param name="context">The identity database context</param>
        /// <param name="userManager">The user manager.</param>
        /// <param name="roleManager">The role manager.</param>
        /// <returns><c>true</c> if users migrated, <c>false</c> otherwise.</returns>
        public static bool MigrateSecurity(TdIdentityContext context, TicketDeskUserManager userManager, TicketDeskRoleManager roleManager)
        {
            roleManager.EnsureDefaultRolesExist();
            var appId =
                context.Database.SqlQuery <Guid>(
                    "select ApplicationId from aspnet_Applications where ApplicationName = 'TicketDesk'").First().ToString();
            var users = context.Database.SqlQuery <LegacyUser>(
                "select UserId, Email, Password, PasswordFormat, Comment from aspnet_Membership where ApplicationId = '" + appId + "' and IsApproved = 1 and IsLockedOut = 0").ToList();
            const string roleQuery = "SELECT r.RoleName FROM aspnet_UsersInRoles u inner join aspnet_Roles r on u.RoleId = r.RoleId WHERE u.UserId = @userId and r.ApplicationId = @appId";

            foreach (var user in users)
            {
                var newUser = new TicketDeskUser
                {
                    UserName    = user.Email,
                    Email       = user.Email,
                    DisplayName = user.Comment,
                };

                var result = user.PasswordFormat == 0 ?
                             userManager.Create(newUser, user.Password) :
                             userManager.Create(newUser);

                if (result.Succeeded)
                {
                    var rolesForUser =
                        context.Database.SqlQuery <string>(roleQuery,
                                                           new SqlParameter("userId", user.UserId),
                                                           new SqlParameter("appId", appId));
                    var newRoles = new List <string>();
                    foreach (var role in rolesForUser)
                    {
                        switch (role.ToLowerInvariant())
                        {
                        case "administrators":
                            newRoles.Add("TdAdministrators");
                            break;

                        case "helpdesk":
                            newRoles.Add("TdHelpDeskUsers");
                            break;

                        case "ticketsubmitters":
                            newRoles.Add("TdInternalUsers");
                            break;

                        default:
                            newRoles.Add("TdPendingUsers");
                            break;
                        }
                    }
                    userManager.AddToRoles(newUser.Id, newRoles.ToArray());
                }
            }
            return(true);
        }
 public UserAdministrationController(TicketDeskUserManager userManager, TicketDeskRoleManager roleManager)
 {
     UserManager = userManager;
     RoleManager = roleManager;
 }
 public UserAdministrationController(TicketDeskUserManager userManager, TicketDeskRoleManager roleManager)
 {
     UserManager = userManager;
     RoleManager = roleManager;
 }