Example #1
0
        /// <summary>
        /// Creates a role for a user after user and profile are created
        /// </summary>
        /// <param name="uid"></param>
        public void CreateProfileRole(int uid)
        {
            LPProfile   lp = db.LPProfiles.Where(id => id.UserID == uid).SingleOrDefault();
            ProfileRole pr = new ProfileRole();

            pr.RoleID    = 1;
            pr.ProfileID = lp.ProfileID;
            db.ProfileRoles.Add(pr);
            db.SaveChanges();
        }
Example #2
0
        public virtual ProfileRole CreateProfileRole(int profileId, int roleItemId)
        {
            var profileRole = new ProfileRole()
            {
                ProfileId    = profileId,
                RoleItemId   = roleItemId,
                CreationDate = DateTime.Now,
                Status       = EntityConstants.ROLEPROFILE_STATUS_ACTIVE
            };

            this.profileRoleRepo.Add(profileRole);
            return(profileRole);
        }
Example #3
0
        public static async Task <RoleProfileViewModel[]> GetRolesAndProfiles <T>(this SiteControllerBase <T> siteController, ISecurityRepository securityRepository)
            where T : PageData
        {
            var profiles = await securityRepository.GetRoleProfilesAsync() ?? new Core.DataModels.Security.Profile[0];

            var getRolesTasks = profiles.Select(async p =>
            {
                var rls = await securityRepository.GetRolesOfProfileAsync(p.Id);

                //put 'basic' right on top
                ProfileRole basicRight = null;
                var basicRightIndex    = -1;
                for (var i = 0; i < rls.Length; i++)
                {
                    if (rls[i].RoleName != "Basfunktioner")
                    {
                        continue;
                    }
                    basicRight      = rls[i];
                    basicRightIndex = i;
                    break;
                }
                if (basicRight == null)
                {
                    return(new RoleProfileViewModel
                    {
                        Id = p.Id,
                        Description = p.Description,
                        ProfileRoles = rls
                    });
                }

                rls[basicRightIndex] = rls[0];
                rls[0] = basicRight;

                return(new RoleProfileViewModel
                {
                    Id = p.Id,
                    Description = p.Description,
                    ProfileRoles = rls
                });
            });

            var profileRoles = await Task.WhenAll(getRolesTasks);

            siteController.ViewData["profileRoles"] = JsonConvert.SerializeObject(profileRoles);

            return(profileRoles);
        }
Example #4
0
        private static async Task <IdentityResult> EnsureRole(IServiceProvider serviceProvider,
                                                              string uid, string role, int ProfileID, bd_lesContext _context)
        {
            IdentityResult IR          = null;
            var            roleManager = serviceProvider.GetService <RoleManager <IdentityRole> >();
            IdentityRole   IRole       = null;

            if (!await roleManager.RoleExistsAsync(role))
            {
                IRole = new IdentityRole(role);
                IR    = await roleManager.CreateAsync(IRole);
            }
            else
            {
                IRole = roleManager.FindByNameAsync(role).Result;
            }

            ProfileRole PR = await _context.ProfileRole.SingleOrDefaultAsync(pr => pr.IdPerfil == ProfileID && pr.RoleId == IRole.Id);

            if (PR == null)
            {
                PR = new ProfileRole {
                    IdPerfil = ProfileID, RoleId = IRole.Id
                };
                _context.Add(PR);
                await _context.SaveChangesAsync();
            }

            var userManager = serviceProvider.GetService <UserManager <ApplicationUsers> >();

            var user = await userManager.FindByIdAsync(uid);

            IR = await userManager.AddToRoleAsync(user, role);

            return(IR);
        }