Пример #1
0
        public async Task <UserRoleAssociation> AddRolePlayerWithoutCheck(Guid userId, int roleId)
        {
            var newUserRoleAssociation = new UserRoleAssociation
            {
                RoleId = roleId,
                UserId = userId,
            };
            await _context.UserRoleAssociations.AddAsync(newUserRoleAssociation);

            await _context.SaveChangesAsync();

            return(newUserRoleAssociation);
        }
Пример #2
0
        public async Task <string> CreateUser(string userName, string email, string password, List <string> userRoles)
        {
            var user = new User(true)
            {
                Email    = email,
                UserName = userName
            };

            using (var session = DataService.OpenSession())
            {
                var result = await UserManager.CreateAsync(user, password);

                if (!result.Succeeded)
                {
                    return("Unable to create user:\n" + String.Join("\n", result.Errors));
                }

                foreach (var role in userRoles)
                {
                    var dbUserRole = session.Get <UserRole>(role.ToString());

                    var roleAssociation = new UserRoleAssociation()
                    {
                        User     = user,
                        UserRole = dbUserRole
                    };

                    DataService.SaveOrUpdate <UserRoleAssociation>(session, roleAssociation);
                }

                session.Flush();
            }
            try
            {
                var emailResult = await SendAcccountFonfirmationEmail(user.Id, userName, email);

                return(emailResult);
            }
            catch (FormatException e)
            {
                return("User created but there was an error sending activation email:\n" + e.Message);
            }
        }
Пример #3
0
        public void UpdateUser(string userId, string userName, string email, List <string> userRoles)
        {
            using (var session = DataService.OpenSession())
            {
                var dbUser = session.Get <User>(userId);
                dbUser.UserName = userName;

                //dbUser.EmailConfirmed = dbUser.Email == email;
                dbUser.Email = email;
                DataService.SaveOrUpdate(session, dbUser);

                var existingUserRoles = session.CreateCriteria <UserRoleAssociation>()
                                        .CreateAlias("User", "user")
                                        .Add(Restrictions.Eq("user.Id", dbUser.Id))
                                        .List <UserRoleAssociation>()
                                        .ToList();

                var rolesToDelete       = existingUserRoles.Where(e => !userRoles.Contains(e.UserRole.Id)).ToList();
                var existingUserRoleIds = existingUserRoles.Select(e => e.UserRole.Id).ToList();
                var userRolesToAdd      = userRoles.Where(e => !existingUserRoleIds.Contains(e)).ToList();

                rolesToDelete.ForEach(u =>
                {
                    DataService.TryDelete(session, u);
                });

                foreach (var role in userRolesToAdd)
                {
                    var dbUserRole = session.Get <UserRole>(role.ToString());

                    var roleAssociation = new UserRoleAssociation()
                    {
                        User     = dbUser,
                        UserRole = dbUserRole
                    };
                    DataService.SaveOrUpdate(session, roleAssociation);
                }

                session.Flush();
            }
        }
Пример #4
0
        public override void SetupDefaults()
        {
            using (var session = DataService.OpenSession())
            {
                var adminUser = session.CreateCriteria <User>()
                                .Add(Restrictions.Eq("UserName", "Admin"))
                                .UniqueResult <User>();
                if (adminUser == null)
                {
                    adminUser = new User(false)
                    {
                        Email          = "*****@*****.**",
                        EmailConfirmed = true,
                        UserName       = "******",
                    };

                    var result = UserManager.CreateAsync(adminUser, "password");
                    result.Wait();

                    if (!result.Result.Succeeded)
                    {
                        throw new Exception("Unable to create user: "******"Admin");
                    }
                }


                var adminRole = session.CreateCriteria <UserRole>()
                                .Add(Restrictions.Eq("Name", "Admin"))
                                .UniqueResult <UserRole>();
                if (adminRole == null)
                {
                    adminRole = new UserRole()
                    {
                        Name        = "Admin",
                        Description = "Administrator"
                    };
                    DataService.SaveOrUpdate(session, adminRole);
                }

                var adminRoleAssociation = session.CreateCriteria <UserRoleAssociation>()
                                           .CreateAlias("User", "user")
                                           .CreateAlias("UserRole", "role")
                                           .Add(Restrictions.Eq("user.Id", adminUser.Id))
                                           .Add(Restrictions.Eq("role.Id", adminRole.Id))
                                           .UniqueResult <UserRoleAssociation>();
                if (adminRoleAssociation == null)
                {
                    adminRoleAssociation = new UserRoleAssociation()
                    {
                        User     = adminUser,
                        UserRole = adminRole
                    };
                    DataService.SaveOrUpdate(session, adminRoleAssociation);
                }

                var menuList1 = session.CreateCriteria <Menu>()
                                .Add(Restrictions.Eq("Event", (int)EventNumber.ViewUsers))
                                .List <Menu>();

                if (menuList1.Count == 0)
                {
                    var menu1 = new Menu()
                    {
                        Event    = EventNumber.ViewUsers,
                        Name     = "Users",
                        Position = 0
                    };

                    DataService.SaveOrUpdate(session, menu1);
                }

                var menuList2 = session.CreateCriteria <Menu>()
                                .Add(Restrictions.Eq("Event", (int)EventNumber.ViewMenus))
                                .List <Menu>();

                if (menuList2.Count == 0)
                {
                    var menu2 = new Menu()
                    {
                        Event    = EventNumber.ViewMenus,
                        Name     = "Menus",
                        Position = 1
                    };
                    DataService.SaveOrUpdate(session, menu2);
                }

                var userRoleMenu = session.CreateCriteria <Menu>()
                                   .Add(Restrictions.Eq("Event", (int)EventNumber.ViewUserRoles))
                                   .Add(Restrictions.IsNull("ParentMenu"))
                                   .UniqueResult <Menu>();
                if (userRoleMenu == null)
                {
                    userRoleMenu = new Menu()
                    {
                        Event    = EventNumber.ViewUserRoles,
                        Name     = "User Roles",
                        Position = 2
                    };
                    DataService.SaveOrUpdate(session, userRoleMenu);
                }

                var allEvents = EventService.EventMenuList.Where(e => e.Value.ActionType != EventType.InputDataView).Select(e => e.Value.GetEventId())
                                .ToList();

                var eras = session.CreateCriteria <EventRoleAssociation>()
                           .CreateAlias("UserRole", "role")
                           .Add(Restrictions.Eq("role.Id", adminRole.Id))
                           .List <EventRoleAssociation>()
                           .ToList();

                if (eras.Count != allEvents.Count)
                {
                    eras.ForEach(e =>
                    {
                        DataService.TryDelete(session, e);
                    });
                    session.Flush();
                    foreach (var evt in allEvents)
                    {
                        var era = new EventRoleAssociation()
                        {
                            Event    = evt,
                            UserRole = adminRole
                        };
                        DataService.SaveOrUpdate(session, era);
                    }
                }
                session.Flush();
            }
        }
Пример #5
0
        public override void SetupDefaults()
        {
            // This code will create an admin user, user role and add all new menu items to the admin user roles, even if you create new ones later.

            // It is recommended to change your admin email and password asap.

            var userManager = Container.Resolve <DefaultUserManager>();

            using (var session = DataService.OpenSession())
            {
                var adminUser = session.CreateCriteria <User>()
                                .Add(Restrictions.Eq("UserName", "Admin"))
                                .UniqueResult <User>();
                if (adminUser == null)
                {
                    adminUser = new User(false)
                    {
                        Email          = "*****@*****.**",
                        EmailConfirmed = true,
                        UserName       = "******",
                    };
                    var result = userManager.CreateAsync(adminUser, "password");
                    result.Wait();

                    if (!result.Result.Succeeded)
                    {
                        throw new Exception("Unable to create user: "******"Admin");
                    }
                }

                var adminRole = session.CreateCriteria <UserRole>()
                                .Add(Restrictions.Eq("Name", "Admin"))
                                .UniqueResult <UserRole>();
                if (adminRole == null)
                {
                    adminRole = new UserRole()
                    {
                        Name        = "Admin",
                        Description = "Administrator"
                    };
                    DataService.SaveOrUpdate(session, adminRole);
                }

                var adminRoleAssociation = session.CreateCriteria <UserRoleAssociation>()
                                           .CreateAlias("User", "user")
                                           .CreateAlias("UserRole", "role")
                                           .Add(Restrictions.Eq("user.Id", adminUser.Id))
                                           .Add(Restrictions.Eq("role.Id", adminRole.Id))
                                           .UniqueResult <UserRoleAssociation>();
                if (adminRoleAssociation == null)
                {
                    adminRoleAssociation = new UserRoleAssociation()
                    {
                        User     = adminUser,
                        UserRole = adminRole
                    };
                    DataService.SaveOrUpdate(session, adminRoleAssociation);
                }

                var systemMenu = session.QueryOver <Menu>().Where(m => m.Name == "System").SingleOrDefault();
                if (systemMenu == null)
                {
                    systemMenu = new Menu()
                    {
                        Name     = "System",
                        Position = -1
                    };
                    DataService.SaveOrUpdate(session, systemMenu);
                }

                var menuList1 = session.CreateCriteria <Menu>()
                                .Add(Restrictions.Eq("Event", (int)EventNumber.ViewUsers))
                                .List <Menu>();

                if (menuList1.Count == 0)
                {
                    var menu1 = new Menu()
                    {
                        Event      = EventNumber.ViewUsers,
                        Name       = "Users",
                        Position   = 0,
                        ParentMenu = systemMenu
                    };

                    DataService.SaveOrUpdate(session, menu1);
                }

                var menuList2 = session.CreateCriteria <Menu>()
                                .Add(Restrictions.Eq("Event", (int)EventNumber.ViewMenus))
                                .List <Menu>();

                if (menuList2.Count == 0)
                {
                    var menu2 = new Menu()
                    {
                        Event      = EventNumber.ViewMenus,
                        Name       = "Menus",
                        Position   = 1,
                        ParentMenu = systemMenu
                    };
                    DataService.SaveOrUpdate(session, menu2);
                }

                var userRoleMenu = session.CreateCriteria <Menu>()
                                   .Add(Restrictions.Eq("Event", (int)EventNumber.ViewUserRoles))
                                   .UniqueResult <Menu>();
                if (userRoleMenu == null)
                {
                    userRoleMenu = new Menu()
                    {
                        Event      = EventNumber.ViewUserRoles,
                        Name       = "User Roles",
                        Position   = 2,
                        ParentMenu = systemMenu
                    };
                    DataService.SaveOrUpdate(session, userRoleMenu);
                }

                var settingsMenu = session.CreateCriteria <Menu>()
                                   .Add(Restrictions.Eq("Event", (int)EventNumber.ModifySystemSettings))
                                   .UniqueResult <Menu>();
                if (settingsMenu == null)
                {
                    settingsMenu = new Menu()
                    {
                        Event      = EventNumber.ModifySystemSettings,
                        Name       = "Settings",
                        Position   = 3,
                        ParentMenu = systemMenu
                    };
                    DataService.SaveOrUpdate(session, settingsMenu);
                }

                var allEvents = EventService.EventList.Where(e => e.Value.ActionType != EventType.InputDataView).Select(e => e.Value.GetEventId())
                                .ToList();

                var eras = session.CreateCriteria <EventRoleAssociation>()
                           .CreateAlias("UserRole", "role")
                           .Add(Restrictions.Eq("role.Id", adminRole.Id))
                           .List <EventRoleAssociation>()
                           .ToList();

                if (eras.Count != allEvents.Count)
                {
                    eras.ForEach(e =>
                    {
                        DataService.TryDelete(session, e);
                    });
                    session.Flush();
                    foreach (var evt in allEvents)
                    {
                        var era = new EventRoleAssociation()
                        {
                            Event    = evt,
                            UserRole = adminRole
                        };
                        DataService.SaveOrUpdate(session, era);
                    }
                }

                session.Flush();
            }
        }
Пример #6
0
 /// <summary>
 /// Constructor
 /// </summary>
 public UserSetup()
 {
     Actions             = new Actions();
     UserRoleAssociation = new UserRoleAssociation();
 }