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);
        }