summaryrefslogtreecommitdiff
path: root/Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/UsuarioRepository.cs
blob: f1f9993922318ee647feb8bace1f77a6c5249311 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
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);
		}
	}
}