Exemplo n.º 1
0
        /// <summary>Faz a autenticação do usuário</sumary>
        public ObjResult Authenticate(string username, string password, string system_flag)
        {
            connection = db.GetCon();
            ObjResult     objResult    = new ObjResult();
            List <string> listMessages = new List <String>();

            try
            {
                connection.Open();

                LogDao     logDao = new LogDao(connection, null);
                UserLogged user   = new UserDao(connection, null).Authenticate(username, password, system_flag);

                // Se a autenticação for bem sucedida, gerar o token JWT
                if (user.id != 0)
                {
                    var tokenHandler    = new JwtSecurityTokenHandler();
                    var configuration   = Builder.GetConfiguration();
                    var key             = Encoding.ASCII.GetBytes(configuration.GetSection("Permissions:SecretKey").Value);
                    var tokenDescriptor = new SecurityTokenDescriptor
                    {
                        Subject = new ClaimsIdentity(new Claim[]
                        {
                            new Claim("use_id", user.id.ToString())
                        }),
                        Expires            = DateTime.UtcNow.AddDays(1),
                        SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
                    };
                    var token = tokenHandler.CreateToken(tokenDescriptor);
                    user.token = tokenHandler.WriteToken(token);

                    logDao.Post(new Log(user.id, "User", "Login", username, ""));
                    objResult.resultStatus = "success";
                    objResult.data         = user;
                }
                else
                {
                    logDao.Post(new Log(0, "User", "ErroLogin", username, username + " : " + password));

                    listMessages.Add("Usuário ou senha inválidos.");
                    objResult.resultStatus   = "error";
                    objResult.resultMessages = listMessages;
                }
            }
            catch (Exception e)
            {
                listMessages.Add(e.Message);
                objResult.resultStatus   = "error";
                objResult.resultMessages = listMessages;
            }
            finally
            {
                connection.Close();
            }

            return(objResult);
        }
        public ObjResult Save(UserPermission obj, int user_id, string action)
        {
            connection = db.GetCon();
            connection.Open();
            int id;

            ObjResult objResult                  = new ObjResult();
            UserPermissionBusiness bus           = new UserPermissionBusiness(connection);
            List <string>          messages_list = new List <string>();

            //validações
            try
            {
                messages_list = bus.Validate(obj, user_id, action);
                if (messages_list.Count > 0)
                {
                    objResult.resultStatus   = "error";
                    objResult.resultMessages = messages_list;
                    connection.Close();
                    return(objResult);
                }
            }
            finally
            {
                connection.Close();
            }

            NpgsqlTransaction transaction = null;

            try
            {
                connection.Open();
                transaction = connection.BeginTransaction();
                UserPermissionDao dao = new UserPermissionDao(connection, transaction);
                id = dao.Post(obj);

                LogDao logDao = new LogDao(connection, transaction);
                logDao.Post(new Log(user_id, "UserPermission", action, id.ToString(), JsonConvert.SerializeObject(obj)));

                objResult.resultStatus = "success";
                objResult.data         = id;
                transaction.Commit();
            }
            catch (Exception ex)
            {
                if (transaction != null)
                {
                    transaction.Rollback();
                }
                throw new Exception(ex.ToString());
            }
            finally
            {
                connection.Close();
            }
            return(objResult);
        }
        public ObjResult ApplyPerfil(dynamic obj, int user_id, string action)
        {
            connection = db.GetCon();
            connection.Open();

            ObjResult objResult                  = new ObjResult();
            UserPermissionBusiness bus           = new UserPermissionBusiness(connection);
            List <string>          resources     = new List <string>();
            List <string>          messages_list = new List <string>();
            int use_id = obj.use_id;

            //validações
            try
            {
                connection.Open();
                UserPermission obj2 = new UserPermission();
                obj2.per_user_id = obj.use_id;

                messages_list = bus.Validate(obj2, user_id, "create");
                if (messages_list.Count > 0)
                {
                    objResult.resultStatus   = "error";
                    objResult.resultMessages = messages_list;
                    connection.Close();
                    return(objResult);
                }
            }
            finally
            {
                connection.Close();
            }

            NpgsqlTransaction transaction = null;

            try
            {
                connection.Open();
                transaction = connection.BeginTransaction();
                UserPermissionDao dao = new UserPermissionDao(connection, transaction);
                resources = dao.ListResources();
                for (int i = 0; i < resources.Count; i++)
                {
                    if (!dao.PermissionExists(use_id, Convert.ToInt32(resources[i])))
                    {
                        UserPermission new_obj = new UserPermission();
                        new_obj.per_resource_id = Convert.ToInt32(resources[i]);
                        new_obj.per_user_id     = use_id;

                        int    id     = dao.Post(new_obj);
                        LogDao logDao = new LogDao(connection, transaction);
                        logDao.Post(new Log(user_id, "UserPermission", "create", id.ToString(), JsonConvert.SerializeObject(obj)));
                    }
                }

                objResult.resultStatus = "success";
                objResult.data         = "ok";
                transaction.Commit();
            }
            catch (Exception ex)
            {
                if (transaction != null)
                {
                    transaction.Rollback();
                }
                throw new Exception(ex.ToString());
            }
            finally
            {
                connection.Close();
            }
            return(objResult);
        }