diff options
Diffstat (limited to 'Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/UsuarioRepository.cs')
| -rw-r--r-- | Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/UsuarioRepository.cs | 305 |
1 files changed, 305 insertions, 0 deletions
diff --git a/Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/UsuarioRepository.cs b/Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/UsuarioRepository.cs new file mode 100644 index 0000000..f1f9993 --- /dev/null +++ b/Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/UsuarioRepository.cs @@ -0,0 +1,305 @@ +using AutoMapper;
+using Gestor.Infrastructure.Entities.Generic;
+using Gestor.Infrastructure.Entities.Seguros;
+using Gestor.Infrastructure.Mappers;
+using Gestor.Infrastructure.Repository.Generic;
+using Gestor.Infrastructure.Repository.Interface;
+using Gestor.Infrastructure.UnitOfWork.Generic;
+using Gestor.Model.API;
+using Gestor.Model.Common;
+using Gestor.Model.Domain.Generic;
+using Gestor.Model.Domain.Seguros;
+using NHibernate;
+using NHibernate.Connection;
+using NHibernate.Impl;
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Data.Common;
+using System.Data.SqlClient;
+using System.Linq;
+using System.Linq.Expressions;
+using System.Reflection;
+using System.Runtime.CompilerServices;
+
+namespace Gestor.Infrastructure.Repository.Logic
+{
+ public class UsuarioRepository : GenericRepository<UsuarioDb>, IUsuarioRepository, IGenericRepository<UsuarioDb>
+ {
+ private readonly GenericUnitOfWork _unitOfWork;
+
+ public UsuarioRepository(GenericUnitOfWork unitOfWork) : base(unitOfWork.Session)
+ {
+ this._unitOfWork = unitOfWork;
+ }
+
+ public Usuario AddUsuarioFromSso(UserSso userSso)
+ {
+ return this.SaveOrUpdate(new Usuario()
+ {
+ SsoId = userSso.Id,
+ IdEmpresa = userSso.IdEmpresa,
+ Nome = userSso.Name,
+ Login = userSso.Username,
+ Senha = userSso.Password,
+ Administrador = false,
+ Excluido = userSso.IsDeleted,
+ PermissaoAggilizador = new long?((long)1)
+ });
+ }
+
+ public void Delete(long id)
+ {
+ UsuarioDb nullable = base.FindEntityById(id);
+ if (nullable.Login.IndexOf("_REM", StringComparison.InvariantCultureIgnoreCase) == -1)
+ {
+ UsuarioDb usuarioDb = nullable;
+ usuarioDb.Login = string.Concat(usuarioDb.Login, "_REM");
+ }
+ nullable.Excluido = new bool?(true);
+ base.Merge(nullable);
+ }
+
+ public List<Usuario> Find(string filter, long idempresa = 0L)
+ {
+ UsuarioRepository.u003cu003ec__DisplayClass11_0 variable = null;
+ ParameterExpression parameterExpression;
+ bool? nullable;
+ IQueryable<UsuarioDb> usuarioDbs;
+ if (idempresa == 0)
+ {
+ IQueryable<UsuarioDb> usuarioDbs1 = base.All();
+ parameterExpression = Expression.Parameter(typeof(UsuarioDb), "x");
+ nullable = null;
+ usuarioDbs = usuarioDbs1.Where<UsuarioDb>(Expression.Lambda<Func<UsuarioDb, bool>>(Expression.OrElse(Expression.AndAlso(Expression.AndAlso(Expression.NotEqual(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_Excluido").MethodHandle)), Expression.Constant(nullable, typeof(bool?))), Expression.Equal(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_Excluido").MethodHandle)), Expression.Convert(Expression.Constant(false, typeof(bool)), typeof(bool?)))), Expression.Call(Expression.Call(Expression.Call(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_Nome").MethodHandle)), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("ToUpper").MethodHandle), Array.Empty<Expression>()), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("Trim").MethodHandle), Array.Empty<Expression>()), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("Contains", new Type[] { typeof(string) }).MethodHandle), new Expression[] { Expression.Field(Expression.Constant(variable, typeof(UsuarioRepository.u003cu003ec__DisplayClass11_0)), FieldInfo.GetFieldFromHandle(typeof(UsuarioRepository.u003cu003ec__DisplayClass11_0).GetField("filter").FieldHandle)) })), Expression.Call(Expression.Call(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_Documento").MethodHandle)), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("Trim").MethodHandle), Array.Empty<Expression>()), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("Contains", new Type[] { typeof(string) }).MethodHandle), new Expression[] { Expression.Field(Expression.Constant(variable, typeof(UsuarioRepository.u003cu003ec__DisplayClass11_0)), FieldInfo.GetFieldFromHandle(typeof(UsuarioRepository.u003cu003ec__DisplayClass11_0).GetField("filter").FieldHandle)) })), new ParameterExpression[] { parameterExpression }));
+ }
+ else
+ {
+ IQueryable<UsuarioDb> usuarioDbs2 = base.All();
+ parameterExpression = Expression.Parameter(typeof(UsuarioDb), "x");
+ nullable = null;
+ usuarioDbs = usuarioDbs2.Where<UsuarioDb>(Expression.Lambda<Func<UsuarioDb, bool>>(Expression.AndAlso(Expression.AndAlso(Expression.AndAlso(Expression.NotEqual(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_Excluido").MethodHandle)), Expression.Constant(nullable, typeof(bool?))), Expression.Equal(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_Excluido").MethodHandle)), Expression.Convert(Expression.Constant(false, typeof(bool)), typeof(bool?)))), Expression.Equal(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_IdEmpresa").MethodHandle)), Expression.Field(Expression.Constant(variable, typeof(UsuarioRepository.u003cu003ec__DisplayClass11_0)), FieldInfo.GetFieldFromHandle(typeof(UsuarioRepository.u003cu003ec__DisplayClass11_0).GetField("idempresa").FieldHandle)))), Expression.OrElse(Expression.Call(Expression.Call(Expression.Call(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_Nome").MethodHandle)), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("ToUpper").MethodHandle), Array.Empty<Expression>()), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("Trim").MethodHandle), Array.Empty<Expression>()), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("Contains", new Type[] { typeof(string) }).MethodHandle), new Expression[] { Expression.Field(Expression.Constant(variable, typeof(UsuarioRepository.u003cu003ec__DisplayClass11_0)), FieldInfo.GetFieldFromHandle(typeof(UsuarioRepository.u003cu003ec__DisplayClass11_0).GetField("filter").FieldHandle)) }), Expression.Call(Expression.Call(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_Documento").MethodHandle)), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("Trim").MethodHandle), Array.Empty<Expression>()), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("Contains", new Type[] { typeof(string) }).MethodHandle), new Expression[] { Expression.Field(Expression.Constant(variable, typeof(UsuarioRepository.u003cu003ec__DisplayClass11_0)), FieldInfo.GetFieldFromHandle(typeof(UsuarioRepository.u003cu003ec__DisplayClass11_0).GetField("filter").FieldHandle)) }))), new ParameterExpression[] { parameterExpression }));
+ }
+ parameterExpression = Expression.Parameter(typeof(UsuarioDb), "x");
+ return usuarioDbs.Select<UsuarioDb, Usuario>(Expression.Lambda<Func<UsuarioDb, Usuario>>(Expression.MemberInit(Expression.New(typeof(Usuario)), new MemberBinding[] { Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(typeof(Usuario).GetMethod("set_IdEmpresa", new Type[] { typeof(long) }).MethodHandle), Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_IdEmpresa").MethodHandle))), Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(typeof(DomainBase).GetMethod("set_Id", new Type[] { typeof(long) }).MethodHandle), Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(EntityBase).GetMethod("get_Id").MethodHandle))), Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(typeof(Usuario).GetMethod("set_Nome", new Type[] { typeof(string) }).MethodHandle), Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_Nome").MethodHandle))), Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(typeof(Usuario).GetMethod("set_Documento", new Type[] { typeof(string) }).MethodHandle), Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_Documento").MethodHandle))), Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(typeof(Usuario).GetMethod("set_Excluido", new Type[] { typeof(bool) }).MethodHandle), Expression.Coalesce(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_Excluido").MethodHandle)), Expression.Constant(false, typeof(bool)))), Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(typeof(Usuario).GetMethod("set_PermissaoAggilizador", new Type[] { typeof(long?) }).MethodHandle), Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_PermissaoAggilizador").MethodHandle))), Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(typeof(Usuario).GetMethod("set_Administrador", new Type[] { typeof(bool) }).MethodHandle), Expression.Coalesce(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_Administrador").MethodHandle)), Expression.Constant(false, typeof(bool)))), Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(typeof(Usuario).GetMethod("set_Visita", new Type[] { typeof(string) }).MethodHandle), Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_Visita").MethodHandle))) }), new ParameterExpression[] { parameterExpression })).ToList<Usuario>();
+ }
+
+ public List<Usuario> Find(long idempresa = 0L)
+ {
+ List<Usuario> usuarios;
+ object connection;
+ DataTable dataTable = new DataTable();
+ SessionFactoryImpl sessionFactory = this._unitOfWork.Session.SessionFactory as SessionFactoryImpl;
+ string str = (idempresa == 0 ? "SELECT * FROM usuario" : "SELECT * FROM usuario WHERE idempresa = @idempresa");
+ try
+ {
+ if (sessionFactory != null)
+ {
+ connection = sessionFactory.ConnectionProvider.GetConnection();
+ }
+ else
+ {
+ connection = null;
+ }
+ using (SqlConnection sqlConnection = connection as SqlConnection)
+ {
+ if (sqlConnection != null)
+ {
+ using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
+ {
+ sqlCommand.CommandText = str;
+ sqlCommand.Parameters.AddWithValue("@idempresa", idempresa);
+ using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter())
+ {
+ sqlDataAdapter.SelectCommand = sqlCommand;
+ sqlDataAdapter.Fill(dataTable);
+ }
+ }
+ }
+ else
+ {
+ usuarios = null;
+ return usuarios;
+ }
+ }
+ usuarios = CustomMap.MapUsuario(dataTable);
+ }
+ catch (Exception exception)
+ {
+ usuarios = null;
+ }
+ return usuarios;
+ }
+
+ public Usuario FindById(long id)
+ {
+ UsuarioDb usuarioDb = base.FindEntityById(id);
+ if (usuarioDb == null)
+ {
+ return null;
+ }
+ return ApplicationMapper.Mapper.Map<UsuarioDb, Usuario>(usuarioDb);
+ }
+
+ public List<Usuario> FindByLoginInteiro(string filter)
+ {
+ return (
+ from x in (
+ from x in base.All()
+ where x.Excluido != null && x.Excluido == (bool?)false && x.Login.Equals(filter)
+ select x).ToList<UsuarioDb>()
+ select new Usuario()
+ {
+ IdEmpresa = x.IdEmpresa,
+ Id = x.Id,
+ Nome = x.Nome,
+ Documento = x.Documento,
+ Excluido = x.Excluido.GetValueOrDefault(),
+ PermissaoAggilizador = x.PermissaoAggilizador,
+ Administrador = x.Administrador.GetValueOrDefault(),
+ Visita = x.Visita
+ } into x
+ where !x.Excluido
+ select x).ToList<Usuario>();
+ }
+
+ private Usuario FindBySso(string ssoId)
+ {
+ UsuarioDb usuarioDb = base.All().FirstOrDefault<UsuarioDb>((UsuarioDb u) => u.SsoId == ssoId);
+ if (usuarioDb == null)
+ {
+ return null;
+ }
+ return ApplicationMapper.Mapper.Map<UsuarioDb, Usuario>(usuarioDb);
+ }
+
+ public bool FindDocUsedByDocumento(string filter, long id, long idempresa)
+ {
+ return base.All().Any<UsuarioDb>((UsuarioDb u) => u.IdEmpresa == idempresa && u.Excluido != null && u.Excluido == (bool?)false && u.Id != id && u.Documento != null && u.Documento.Replace(".", string.Empty).Replace("-", string.Empty).Replace("/", string.Empty).Equals(filter));
+ }
+
+ public Usuario FindFromSso(UserSso userSso)
+ {
+ UsuarioRepository.u003cu003ec__DisplayClass4_0 variable = null;
+ if (userSso.IsDeleted)
+ {
+ return null;
+ }
+ IQueryable<UsuarioDb> usuarioDbs = base.All();
+ ParameterExpression parameterExpression = Expression.Parameter(typeof(UsuarioDb), "u");
+ UsuarioDb usuarioDb = usuarioDbs.FirstOrDefault<UsuarioDb>(Expression.Lambda<Func<UsuarioDb, bool>>(Expression.Call(Expression.Call(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_Login").MethodHandle)), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("ToUpper").MethodHandle), Array.Empty<Expression>()), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("Equals", new Type[] { typeof(string) }).MethodHandle), new Expression[] { Expression.Call(Expression.Property(Expression.Field(Expression.Constant(variable, typeof(UsuarioRepository.u003cu003ec__DisplayClass4_0)), FieldInfo.GetFieldFromHandle(typeof(UsuarioRepository.u003cu003ec__DisplayClass4_0).GetField("userSso").FieldHandle)), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UserSso).GetMethod("get_Username").MethodHandle)), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("ToUpper").MethodHandle), Array.Empty<Expression>()) }), new ParameterExpression[] { parameterExpression }));
+ if (usuarioDb == null)
+ {
+ return null;
+ }
+ return ApplicationMapper.Mapper.Map<UsuarioDb, Usuario>(usuarioDb);
+ }
+
+ public Usuario FindUsuario(string login)
+ {
+ UsuarioDb usuarioDb = (
+ from x in base.All()
+ where x.Excluido != null && x.Excluido == (bool?)false
+ select x).FirstOrDefault<UsuarioDb>((UsuarioDb x) => x.Login == login);
+ return ApplicationMapper.Mapper.Map<UsuarioDb, Usuario>(usuarioDb);
+ }
+
+ public Usuario Merge(Usuario usuario)
+ {
+ UsuarioDb usuarioDb = ApplicationMapper.Mapper.Map<Usuario, UsuarioDb>(usuario);
+ base.Merge(usuarioDb);
+ return ApplicationMapper.Mapper.Map<UsuarioDb, Usuario>(usuarioDb);
+ }
+
+ public List<PermissaoAggilizador> PermissaoAggilizador()
+ {
+ List<PermissaoAggilizador> list;
+ object connection;
+ SessionFactoryImpl sessionFactory = this._unitOfWork.Session.SessionFactory as SessionFactoryImpl;
+ DataTable dataTable = new DataTable();
+ try
+ {
+ if (sessionFactory != null)
+ {
+ connection = sessionFactory.ConnectionProvider.GetConnection();
+ }
+ else
+ {
+ connection = null;
+ }
+ using (SqlConnection sqlConnection = connection as SqlConnection)
+ {
+ if (sqlConnection != null)
+ {
+ using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
+ {
+ sqlCommand.CommandText = "SELECT * FROM Permissao where IdPermissao != 4";
+ using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter())
+ {
+ sqlDataAdapter.SelectCommand = sqlCommand;
+ sqlDataAdapter.Fill(dataTable);
+ }
+ }
+ }
+ }
+ list = (
+ from x in dataTable.AsEnumerable().ToList<DataRow>()
+ select new PermissaoAggilizador()
+ {
+ Id = x.Field<long>("IdPermissao"),
+ Descricao = x.Field<string>("Nome").ToUpper()
+ }).ToList<PermissaoAggilizador>();
+ }
+ catch (Exception exception)
+ {
+ list = new List<PermissaoAggilizador>()
+ {
+ new PermissaoAggilizador()
+ {
+ Id = (long)1,
+ Descricao = "ADMINISTRADOR"
+ },
+ new PermissaoAggilizador()
+ {
+ Id = (long)2,
+ Descricao = "NORMAL"
+ },
+ new PermissaoAggilizador()
+ {
+ Id = (long)3,
+ Descricao = "LIMITADO"
+ }
+ };
+ }
+ return list;
+ }
+
+ public Usuario SaveOrUpdate(Usuario usuario)
+ {
+ UsuarioDb usuarioDb = ApplicationMapper.Mapper.Map<Usuario, UsuarioDb>(usuario);
+ this.SaveOrUpdate(usuarioDb);
+ return ApplicationMapper.Mapper.Map<UsuarioDb, Usuario>(usuarioDb);
+ }
+
+ public Usuario SsoId(AuthSso sso)
+ {
+ if (sso.Code != 200)
+ {
+ return null;
+ }
+ return this.FindBySso(sso.Data.User.Id);
+ }
+
+ public Usuario ValidateLogin(string login, string password)
+ {
+ UsuarioDb usuarioDb = this._unitOfWork.Session.CreateQuery("FROM UsuarioDb WHERE (removido IS NULL OR removido != '1') AND (inativo = '0' OR inativo IS NULL)").List<UsuarioDb>().FirstOrDefault<UsuarioDb>((UsuarioDb u) => {
+ if (!string.Equals(u.Login.Trim(), login.Trim(), StringComparison.CurrentCultureIgnoreCase))
+ {
+ return false;
+ }
+ return u.Senha == password;
+ });
+ return ApplicationMapper.Mapper.Map<UsuarioDb, Usuario>(usuarioDb);
+ }
+ }
+}
\ No newline at end of file |