Example #1
0
        // Enable initialization with an instance of ApplicationUser:
        public RoleService(ApplicationUser user)
            : this()
        {
            UserName  = user.UserName;
            FirstName = user.FirstName;
            LastName  = user.LastName;

            var Db = new FClubBarcelonaContext();

            var allRoles = Db.Roles;

            foreach (var role in allRoles)
            {
                // An EditorViewModel will be used by Editor Template:
                var rvm = new SelectRoleEditorViewModel(role);
                Roles.Add(rvm);
            }

            // Set the Selected property to true for those roles for
            // which the current user is a member:
            foreach (var userRole in user.Roles)
            {
                var checkUserRole =
                    Roles.Find(r => r.Id == userRole.RoleId);
                checkUserRole.Selected = true;
            }
        }
        public ActionResult Edit(string id)
        {
            var Db   = new ApplicationDbContext();
            var user = Db.Users.FirstOrDefault(u => u.Id == id);

            var roleList = new List <SelectRoleEditorViewModel>();

            var allRoles = Db.Roles;

            foreach (var role in allRoles)
            {
                //var roleName = allRoles.FirstOrDefault(r => r.Id == role.Id).Name.ToList();

                var rvm = new SelectRoleEditorViewModel()
                {
                    RoleId   = role.Id,
                    RoleName = role.Name
                };
                roleList.Add(rvm);
            }

            ViewBag.Name = new SelectList(db.Roles.ToList(), "Name", "Name");
            var model = new EditUserViewModel(user);

            model.Roles = roleList;
            var roles = Roles();

            model.RoleNames = GetRolesListItems(roles);
            return(View(model));
        }
        private List <SelectRoleEditorViewModel> GetViewModelRoles()
        {
            var roles = new List <SelectRoleEditorViewModel>();

            foreach (var role in RoleManager.Roles)
            {
                // An EditorViewModel will be used by Editor Template:
                var srevm = new SelectRoleEditorViewModel(role);
                roles.Add(srevm);
            }

            return(roles);
        }
        public ActionResult _ListaPerfis(string usuarioId)
        {
            try
            {
                var model = _gerenciadorDeIdentity.BuscaUsuario(usuarioId);
                var usuario = Mapper.Map<UsuarioIdentity, UsuarioViewModel>(model);

                var usuarioRoleViewModel = new SelectUsuarioRolesEditorViewModel(usuario);
                
                var todasAsRoles = _gerenciadorDeIdentity.RecuperarRoles();

                foreach (var role in todasAsRoles)
                {
                    var roleViewModel = new SelectRoleEditorViewModel(role);
                    usuarioRoleViewModel.AdicionarRole(roleViewModel);
                }

                foreach (var usuarioRole in usuario.Roles)
                {
                    var roleAux = _gerenciadorDeIdentity.BuscarRole(usuarioRole.RoleId);

                    var usuarioRoleVinculado =
                        usuarioRoleViewModel.Roles.Find(r => r.Nome == roleAux.Name);
                    usuarioRoleVinculado.Selected = true;
                }

                return View(usuarioRoleViewModel);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }