Esempio n. 1
0
        public async Task <IActionResult> AddEdit(string id, UsuarioAddEditVM model)
        {
            ModelState.Remove("IdUsuarioRegistro");
            ModelState.Remove("DataHoraRegistro");
            ModelState.Remove("ConfirmaSenha");

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var data = model.ToData();

            if (String.IsNullOrEmpty(id))
            {
                data.HashSenha         = model.Senha.Encrypt();
                data.IdUsuarioRegistro = Convert.ToDecimal(HttpContextHelper.GetAuthUserId(HttpContext));
                data.DhRegistro        = Convert.ToDecimal(DateTime.Now.ToString("yyyyMMddHHmmss"));
                data.StUsuario         = EnumHelper.SituacaoUsuario.UA9.ToString();

                _usuarioRepo.Add(data);
            }
            else
            {
                _usuarioRepo.Update(data);
            }

            await _uow.CommitAsync();

            return(RedirectToAction("Index"));
        }
Esempio n. 2
0
        public async Task <IActionResult> AddEdit(int Id, UsuarioAddEditVM model)
        {
            if (!ModelState.IsValid)
            {
                await GetPerfis(model);

                return(View(model));
            }
            var usr = model.ToData();

            usr.Perfis = await MontarPerfil(model.PerfisId);

            if (Id == 0)
            {
                usr.Senha = usr.Senha.Encrypt();
                _usuarioRepository.Add(usr);
            }
            else
            {
                //tratar para não mostrar a senha no browser
                //usr.Id = Id;
                //usr.Senha = _usuarioRepository.Get(Id).Senha;
                //usr.DataAlteracao = DateTime.Now;
                // com model.toData não funcionou!!!
                var x = _usuarioRepository.Get(Id);
                x.Nome          = model.Nome;
                x.Genero        = (Genero)model.Genero;
                x.Email         = model.Email;
                x.DataAlteracao = DateTime.Now;
                x.Perfis        = usr.Perfis;

                //Usuario u = new Usuario()
                //{
                //    Id = Id,
                //    Nome = model.Nome,
                //    Genero = (Genero)model.Genero,
                //    Email = model.Email,
                //    DataAlteracao = DateTime.Now
                //};

                _usuarioRepository.Update(x);
            }
            await _uow.CommitAsync();

            return(RedirectToAction("Index"));
        }
Esempio n. 3
0
        public async Task <IActionResult> AddNewUser(string returnUrl, UsuarioAddEditVM model)
        {
            var data = model.ToData();

            data.HashSenha  = model.Senha.Encrypt();
            data.DhRegistro = Convert.ToDecimal(DateTime.Now.ToString("yyyyMMddHHmmss"));
            data.StUsuario  = EnumHelper.SituacaoUsuario.UA9.ToString();

            _usuarioRepo.Add(data);
            data.IdUsuarioRegistro = data.IdUsuario;

            await _uow.CommitAsync();

            await Task.Run(async() =>
            {
                int userId = Convert.ToInt32(data.IdUsuario);

                var _userToSend = new EmailFormModel()
                {
                    FromEmail = data.EmailUsuario,
                    FromName  = data.NmUsuario
                };

                // Credenciais para o envio do email
                var credentials = _credential.GetEmailCredentials();

                var credEmail = credentials.Where(k => k.Key == "Email").Select(e => e.Value).ToList().FirstOrDefault();
                var credPass  = credentials.Where(k => k.Key == "Password").Select(e => e.Value).ToList().FirstOrDefault();
                var credHost  = credentials.Where(k => k.Key == "Host").Select(e => e.Value).ToList().FirstOrDefault();
                var credPort  = credentials.Where(k => k.Key == "Port").Select(e => e.Value).ToList().FirstOrDefault();

                var _sender = new Sender(credEmail, credPass, credHost, Convert.ToInt16(credPort));

                await _sender.SendConfirmation(_userToSend, userId);
            });

            if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
            {
                return(Redirect(returnUrl));
            }
            else
            {
                return(View("Success", data));
            };
        }