Beispiel #1
0
        public Task MapUserToSociety(long userId, long societyId, long roleId)
        {
            var taskResult = Task.Run(() =>
            {
                using (var context = new DbContext())
                {
                    if (context.MapUserToSocieties.Any(m => m.UserId == userId && m.SocietyId == societyId && m.RoleId == roleId))
                    {
                        return;
                    }

                    var map = new MapUserToSociety()
                    {
                        UserId = userId, SocietyId = societyId, RoleId = roleId
                    };
                    context.MapUserToSocieties.Add(map);
                    context.SaveChanges();
                }
            });

            return(taskResult);
        }
Beispiel #2
0
        public Task <ApplicationUser> CreateSocietyUser(ApplicationUser user, long societyId, ApplicationUser currentUser)
        {
            var taskResult = Task.Run(async() =>
            {
                using (var context = new DbContext())
                {
                    //check if user exist with username before
                    var existing = context
                                   .Users
                                   .FirstOrDefault(u => u.UserName == user.UserName);

                    if (existing != null)
                    {
                        //Map user to super admin role for the complex. Super Admin = 3
                        var roleexist = existing.Roles.FirstOrDefault(r => r.RoleId == ProgramCommon.SuperAdmin);
                        if (roleexist == null)
                        {
                            existing.Roles.Add(new ApplicationUserRole()
                            {
                                RoleId = ProgramCommon.SuperAdmin
                            });
                        }

                        var map = new MapUserToSociety()
                        {
                            UserId = existing.Id, SocietyId = societyId, RoleId = ProgramCommon.SuperAdmin
                        };
                        context.MapUserToSocieties.Add(map);
                        context.SaveChanges();

                        return(existing);
                    }
                    else
                    {
                        user.Roles.Add(new ApplicationUserRole()
                        {
                            RoleId = ProgramCommon.Frontend
                        });
                        //Map user to super admin role for the complex. Super Admin = 3
                        user.Roles.Add(new ApplicationUserRole()
                        {
                            RoleId = ProgramCommon.SuperAdmin
                        });

                        var passwordHash   = new PasswordHasher();
                        user.PasswordHash  = passwordHash.HashPassword(user.PhoneNumber);
                        user.SecurityStamp = Guid.NewGuid().ToString();

                        await base.CreateAsync(user);

                        var map = new MapUserToSociety()
                        {
                            UserId = user.Id, SocietyId = societyId, RoleId = ProgramCommon.SuperAdmin
                        };
                        context.MapUserToSocieties.Add(map);
                        context.SaveChanges();

                        return(user);
                    }
                }
            });

            return(taskResult);
        }