public async Task CreateDefaultSuperAdmin()
        {
            try
            {
                await _roles.GenerateRolesFromPagesAsync();

                ApplicationUser superAdmin = new ApplicationUser();
                superAdmin.Email          = _superAdminDefaultOptions.Email;
                superAdmin.UserName       = superAdmin.Email;
                superAdmin.EmailConfirmed = true;

                var result = await _userManager.CreateAsync(superAdmin, _superAdminDefaultOptions.Password);

                if (result.Succeeded)
                {
                    //add to user profile
                    UserProfile profile = new UserProfile();
                    profile.FirstName         = "Super";
                    profile.LastName          = "Admin";
                    profile.Email             = superAdmin.Email;
                    profile.ApplicationUserId = superAdmin.Id;
                    profile.GroupId           = 5;
                    profile.ProfilePicture    = "/upload/admin.png";
                    await _context.UserProfile.AddAsync(profile);

                    await _context.SaveChangesAsync();

                    await _roles.AddToRoles(superAdmin.Id);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Example #2
0
        public async Task <IActionResult> GetRole()
        {
            await _roles.GenerateRolesFromPagesAsync();

            List <IdentityRole> Items = new List <IdentityRole>();

            Items = _roleManager.Roles.ToList();
            int Count = Items.Count();

            return(Ok(new { Items, Count }));
        }
Example #3
0
        public async Task <IActionResult> Insert([FromBody] CrudViewModel <UserProfile> payload)
        {
            await _roles.GenerateRolesFromPagesAsync();

            var roles = _roleManager.Roles.ToList();

            UserProfile register = payload.value;

            if (register.Password.Equals(register.ConfirmPassword))
            {
                ApplicationUser user = new ApplicationUser()
                {
                    Email = register.Email, UserName = register.Email, EmailConfirmed = true
                };
                var result = await _userManager.CreateAsync(user, register.Password);

                if (result.Succeeded)
                {
                    register.Password          = user.PasswordHash;
                    register.ConfirmPassword   = user.PasswordHash;
                    register.ApplicationUserId = user.Id;

                    _context.UserProfile.Add(register);
                    await _context.SaveChangesAsync();

                    //assign role to user!
                    List <MembershipRole> Items = _context.MembershipRole
                                                  .Where(x => x.MembershipId.Equals(register.MembershipId))
                                                  .ToList();
                    foreach (var role in roles)
                    {
                        bool IsHaveAccess = false;
                        foreach (var item in Items)
                        {
                            if (item.RoleName == role.Name)
                            {
                                IsHaveAccess = true;
                            }
                        }
                        if (IsHaveAccess)
                        {
                            await _userManager.AddToRoleAsync(user, role.Name);
                        }
                        else
                        {
                            await _userManager.RemoveFromRoleAsync(user, role.Name);
                        }
                    }
                }
            }
            return(Ok(register));
        }
Example #4
0
        public async Task CreateDefaultSuperAdmin()
        {
            try
            {
                await _roles.GenerateRolesFromPagesAsync();

                var roles = _roleManager.Roles.ToList();
                //add memberships
                Membership MS_Admin = new Membership();
                MS_Admin.Name = "SuperAdmin";
                await _context.Membership.AddAsync(MS_Admin);

                await _context.SaveChangesAsync();

                Membership MS_User = new Membership();
                MS_User.Name = "User";
                await _context.Membership.AddAsync(MS_User);

                await _context.SaveChangesAsync();

                //add membership role
                foreach (var item in roles)
                {
                    MembershipRole MSRole = new MembershipRole();
                    MSRole.MembershipId = MS_Admin.MembershipId;
                    MSRole.RoleName     = item.Name;
                    await _context.MembershipRole.AddAsync(MSRole);

                    await _context.SaveChangesAsync();
                }

                //add users
                ApplicationUser superAdmin = new ApplicationUser();
                superAdmin.Email          = _superAdminDefaultOptions.Email;
                superAdmin.UserName       = superAdmin.Email;
                superAdmin.EmailConfirmed = true;

                var result = await _userManager.CreateAsync(superAdmin, _superAdminDefaultOptions.Password);

                if (result.Succeeded)
                {
                    //add to user profile
                    UserProfile profile = new UserProfile();
                    profile.FirstName         = "Super";
                    profile.LastName          = "Admin";
                    profile.Email             = superAdmin.Email;
                    profile.ApplicationUserId = superAdmin.Id;
                    profile.MembershipId      = MS_Admin.MembershipId;
                    await _context.UserProfile.AddAsync(profile);

                    await _context.SaveChangesAsync();

                    await _roles.AddToRoles(superAdmin.Id);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Example #5
0
        // GET: Authentication/UserProfiles/ChangeRole/5
        public async Task <IActionResult> ChangeRole(int?id)
        {
            var userProfile = await _context.UserProfile
                              .FirstOrDefaultAsync(m => m.UserProfileId == id);

            await _roles.GenerateRolesFromPagesAsync();

            var user = await _userManager.FindByIdAsync(userProfile.VinarishUserId);

            var roles = _roleManager.Roles.ToList();
            UserRoleViewModel Item = new UserRoleViewModel
            {
                Profile        = userProfile,
                VinarishUserId = userProfile.VinarishUserId
            };

            foreach (var role in roles)
            {
                bool isInRole = (await _userManager.IsInRoleAsync(user, role.Name)) ? true : false;
                Item.RoleList.Add(role.Name, isInRole);
            }
            return(View(Item));
        }
Example #6
0
        public async Task CreateUserProfile(ApplicationUser existingUser, string firstName, string lastName)
        {
            var profile = _context.UserProfile.SingleOrDefault(x => x.ApplicationUserId.Equals(existingUser.Id));

            if (profile == null)
            {
                await _roles.GenerateRolesFromPagesAsync();

                //add to user profile
                profile = new UserProfile
                {
                    FirstName         = firstName,
                    LastName          = lastName,
                    Email             = existingUser.Email,
                    ApplicationUserId = existingUser.Id
                };
                await _context.UserProfile.AddAsync(profile);

                await _context.SaveChangesAsync();

                await _roles.AddToRoles(existingUser.Id);
            }
        }
Example #7
0
        public async Task CreateDefaultSuperAdmin()
        {
            try
            {
                await _roles.GenerateRolesFromPagesAsync();

                VinarishUser superAdmin = new VinarishUser
                {
                    Email = _superAdminDefaultOptions.Email
                };
                superAdmin.UserName       = superAdmin.Email;
                superAdmin.EmailConfirmed = true;

                IdentityResult result = await _userManager.CreateAsync(superAdmin, _superAdminDefaultOptions.Password);

                if (result.Succeeded)
                {
                    //add to user profile
                    UserProfile profile = new UserProfile
                    {
                        FirstName      = "Farzad",
                        LastName       = "Motallebizaade",
                        Email          = superAdmin.Email,
                        VinarishUserId = superAdmin.Id
                    };
                    await _context.UserProfile.AddAsync(profile);

                    await _context.SaveChangesAsync();

                    await _roles.AddToRoles(superAdmin.Id);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }