private User insert()
        {
            this._user.Email = this._user.Email.Trim();
            if (string.IsNullOrWhiteSpace(this._password))
            {
                throw new AppException("A senha é requerida");
            }

            if (_context.users.Any(x => x.Email == this._user.Email))
            {
                throw new AppException("O Email - " + this._user.Email + " - já está cadastrado");
            }

            byte[] passwordHash, passwordSalt;
            appAccount.CreatePasswordHash(this._password, out passwordHash, out passwordSalt);

            this._user.PasswordHash = passwordHash;
            this._user.PasswordSalt = passwordSalt;
            this._user.IsAdmin      = false;

            _context.users.Add(this._user);
            _context.SaveChanges();

            return(this._user);
        }
Exemple #2
0
        public IActionResult criarSenha([FromBody] NovaSenhaDTO token)
        {
            try
            {
                if (!this._service.ValidaSenha(token.Senha))
                {
                    return(BadRequest(new { message = "Senha fornecida não satisfaz condições pré definidas!" }));
                }

                var usuario = this._service.ValidarToken(token);

                if (usuario == null)
                {
                    return(BadRequest());
                }

                byte[]     passwordHash, passwordSalt;
                AppAccount appAccount = new AppAccount();
                appAccount.CreatePasswordHash(token.Senha, out passwordHash, out passwordSalt);

                usuario.PasswordHash = passwordHash;
                usuario.PasswordSalt = passwordSalt;

                //var userDTO = this._mapper.Map<UserDTO>(usuario);
                //this.atualizarUsuario(userDTO);
                this._service.Update(usuario);

                return(Ok());
            }
            catch (Exception ex)
            {
                return(BadRequest(new { message = ex.Message }));
            }
        }
        private void Update()
        {
            var user = _context.users.Find(this._user.Id);

            if (user == null)
            {
                throw new AppException("Usuário não encontrado");
            }

            if (this._user.Email != user.Email)
            {
                if (_context.users.Any(a => a.Email == this._user.Email))
                {
                    throw new AppException("O Email - " + this._user.Email + " - já está cadastrado");
                }
            }

            user.Nome      = this._user.Nome;
            user.Sobrenome = this._user.Sobrenome;
            user.Email     = this._user.Email;
            user.TelCel    = this._user.TelCel;
            user.TelRes    = this._user.TelRes;

            if (!string.IsNullOrWhiteSpace(this._password))
            {
                byte[] passwordHash, passwordSalt;
                appAccount.CreatePasswordHash(this._password, out passwordHash, out passwordSalt);

                user.PasswordHash = passwordHash;
                user.PasswordSalt = passwordSalt;
            }

            _context.users.Update(user);
            _context.SaveChanges();
        }
        private void CreateHash()
        {
            byte[] passwordHash, passwordSalt;
            appAccount.CreatePasswordHash(this._newPassword, out passwordHash, out passwordSalt);

            this._user.PasswordHash = passwordHash;
            this._user.PasswordSalt = passwordSalt;

            this.UpdateUser();
        }
        public Usuario cadastrar()
        {
            this.validarCadastro();

            Usuario usuario = this._context.Usuario.Where(w => w.Email == this._cadastroUsuario.Email).FirstOrDefault();

            // Cadastrar um novo usuário caso não exista
            if (usuario == null)
            {
                this._password = (this._cadastroUsuario.IdEmpresaSelecionada.ToString() + this._cadastroUsuario.Email.Substring(2, 5) + "*" + DateTime.Now.Minute.ToString()).Trim();

                byte[] passwordHash, passwordSalt;
                appAccount.CreatePasswordHash(this._password, out passwordHash, out passwordSalt);

                usuario = new Usuario()
                {
                    Nome         = this._cadastroUsuario.Nome,
                    Email        = this._cadastroUsuario.Email,
                    PasswordHash = passwordHash,
                    PasswordSalt = passwordSalt,
                    Status       = (int)AppSettings.KdStatus.Ativo
                };

                this._context.Usuario.Add(usuario);
                this._context.SaveChanges();
            }

            UsuarioEmpresa usuarioEmpresa = this._context.UsuarioEmpresa.Where(w => w.EmpresaId == this._cadastroUsuario.IdEmpresaSelecionada && w.UsuarioId == usuario.Id).FirstOrDefault();
            // 0 = Cliente | 1 = Proprietario
            var isProprietario = this._context.Empresa.FirstOrDefault(x => x.Id == this._cadastroUsuario.IdEmpresaSelecionada).EmpresaProprietaria;
            var isPrimario     = this._context.UsuarioEmpresa.Where(x => x.EmpresaId == this._cadastroUsuario.IdEmpresaSelecionada).ToList().Count() > 0 ? 0 : 1;

            if (usuarioEmpresa == null)
            {
                usuarioEmpresa = new UsuarioEmpresa()
                {
                    UsuarioId = usuario.Id,
                    EmpresaId = this._cadastroUsuario.IdEmpresaSelecionada,
                    PerfilId  = isProprietario > 0 ? this._cadastroUsuario.PerfilId : 1,
                    Primario  = isPrimario
                };

                this._context.UsuarioEmpresa.Add(usuarioEmpresa);
                this._context.SaveChanges();
            }

            return(usuario);
        }