public MembershipContext ValidateUser(string email, string senha, int perfilModulo)
        {
            var membershipCtx = new MembershipContext();

            var usuario = _usuarioRep.GetSingleByEmail(email, perfilModulo);

            if (usuario != null && isUserValid(usuario, senha))
            {
                membershipCtx.Usuario = usuario;

                string[] perfil = { usuario.Perfil.DescPerfil };

                var identity = new GenericIdentity(usuario.UsuarioEmail);

                identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, usuario.Id.ToString(), ClaimValueTypes.String));

                membershipCtx.Principal = new GenericPrincipal(identity, perfil);
            }

            return(membershipCtx);
        }
示例#2
0
        public HttpResponseMessage Inserir(HttpRequestMessage request, UsuarioViewModel novoUsuario)
        {
            return(CreateHttpResponse(request, () =>
            {
                HttpResponseMessage response = null;

                var usuarioEmail = _usuarioRep.GetSingleByEmail(novoUsuario.UsuarioEmail, novoUsuario.PerfilId);

                if (!ModelState.IsValid)
                {
                    response = request.CreateResponse(HttpStatusCode.BadRequest, new { success = false });
                }
                else if (usuarioEmail != null)
                {
                    ModelState.AddModelError("Email Existente", "Email:" + usuarioEmail.UsuarioEmail + " já existe .");
                    response = request.CreateResponse(HttpStatusCode.BadRequest,
                                                      ModelState.Keys.SelectMany(k => ModelState[k].Errors)
                                                      .Select(m => m.ErrorMessage).ToArray());
                }
                else
                {
                    var usuario = _usuarioRep.GetSingle(int.Parse(HttpContext.Current.User.Identity.GetUserId()));

                    var booNovoAdmin = novoUsuario.PessoaId == 0 && novoUsuario.Senha == "ABCD1234" && novoUsuario.PerfilId == 1;

                    if (booNovoAdmin)
                    {
                        const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
                        var random = new Random();
                        var senhaRand = new string(
                            Enumerable.Repeat(chars, 8)
                            .Select(s => s[random.Next(s.Length)])
                            .ToArray());

                        novoUsuario.Senha = senhaRand;

                        var novaPessoa = new Pessoa
                        {
                            Ativo = true,
                            DtCriacao = DateTime.Now,
                            UsuarioCriacao = usuario,
                            TipoPessoa = TipoPessoa.PessoaFisica
                        };
                        _pessoaRep.Add(novaPessoa);
                        _unitOfWork.Commit();
                        //novoUsuario.PessoaId = _pessoaRep.GetAll().OrderByDescending(u => u.Id).FirstOrDefault().Id;
                        novoUsuario.PessoaId = novaPessoa.Id;
                    }

                    var user = _membershipService.CreateUser(novoUsuario.UsuarioNome, novoUsuario.UsuarioEmail, novoUsuario.Senha,
                                                             novoUsuario.PerfilId, novoUsuario.PessoaId, usuario.Id, novoUsuario.FlgMaster);

                    if (booNovoAdmin)
                    {
                        var template = _templateEmailRep.GetSingle(3).Template;
                        _emailService.EnviaEmail(novoUsuario.UsuarioEmail, "",
                                                 _emailService.MontaEmail(novoUsuario, template), "Novo Usuário - Economiza Já");
                    }

                    // Update view model
                    var usuarioVM = Mapper.Map <Usuario, UsuarioViewModel>(user);
                    response = request.CreateResponse(HttpStatusCode.Created, usuarioVM);
                }

                return response;
            }));
        }