- added to FunctionBase: bool register_default_models (default true)

if true then winix will add default models for ezc templates such as "request", "item", etc.
- some methods from templates/item.cpp moved to Item
master
Tomasz Sowa 2 years ago
parent e7c7324058
commit 79eda7abb0

@ -344,6 +344,7 @@ bool App::Init()
model_connector.set_winix_locale(&TemplatesFunctions::locale);
model_connector.set_winix_session_manager(&session_manager);
model_connector.set_winix_time_zones(&system.time_zones);
model_connector.set_winix_pattern_cacher(&TemplatesFunctions::pattern_cacher);
if( !TryToMakeDatabaseMigration() )
return false;
@ -846,6 +847,24 @@ void App::CheckPostRedirect()
}
void App::AddDefaultModels()
{
if( !cur.request->send_bin_stream && !cur.request->return_json )
{
if( cur.request->function && cur.request->function->register_default_models )
{
cur.request->models.Add(L"request", cur.request);
if( cur.request->is_item )
{
cur.request->models.Add(L"item", cur.request->item);
}
}
}
}
// !! IMPROVE ME change to a better name
// may ProcessRequest()? but probably it is already defined...
// this method needs some refactoring
@ -886,6 +905,8 @@ void App::Make()
if( !cur.request->redirect_to.empty() )
return;
AddDefaultModels();
if( cur.request->status == WINIX_ERR_OK )
functions.MakeFunction();

@ -194,6 +194,7 @@ private:
void SetLocale();
void CheckPostRedirect();
void MakePage();
void AddDefaultModels();
void Make();
void SaveSessionsIfNeeded(); // !! IMPROVE ME wywalic do menagera sesji??
void LogAccess();

@ -1389,7 +1389,7 @@ using namespace misc_private;
tmp_qencode.clear();
QEncode(in, tmp_qencode);
tmp_qencode.to_string(out, clear);
tmp_qencode.to_str(out, clear);
tmp_qencode.clear();
}

@ -771,7 +771,7 @@ void UrlEncode(char c, StringType & out, bool clear_out = true)
static pt::TextStream tmp;
UrlEncode(c, tmp);
tmp.to_string(out, clear_out);
tmp.to_str(out, clear_out);
}
@ -882,6 +882,7 @@ void QEncode(const std::wstring & in, pt::TextStreamBase<char_type, stack_size,
}
// no thread safe
void QEncode(const std::wstring & in, std::string & out, bool clear = true);

@ -94,6 +94,11 @@ public:
void Str(const StringType & str);
void Str(const StringType && str);
void to_str(std::string & str, bool clear_string = true) const;
void to_str(std::wstring & str, bool clear_string = true) const;
void to_string(std::string & str, bool clear_string = true) const;
void to_string(std::wstring & str, bool clear_string = true) const;
CharType operator[](size_t index);
TextStream & operator<<(const char * str);
@ -115,6 +120,8 @@ public:
TextStream & operator<<(const pt::Space & space);
TextStream & operator<<(const pt::Date & date);
TextStream & operator<<(const TextStream & stream);
template<typename arg_char_type, size_t arg_stack_size, size_t arg_heap_block_size>
TextStream & operator<<(const pt::TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size> & arg);
@ -233,10 +240,59 @@ void TextStream<StringType>::Str(const StringType & str)
template<class StringType>
void TextStream<StringType>::Str(const StringType && str)
{
buffer = str;
buffer = std::move(str);
}
template<class StringType>
void TextStream<StringType>::to_str(std::string & str, bool clear_string) const
{
return to_string(str, clear_string);
}
template<class StringType>
void TextStream<StringType>::to_str(std::wstring & str, bool clear_string) const
{
return to_string(str, clear_string);
}
template<class StringType>
void TextStream<StringType>::to_string(std::string & str, bool clear_string) const
{
if( clear_string )
str.clear();
if constexpr (sizeof(typename StringType::value_type) == sizeof(char))
{
str.append(buffer);
}
else
{
pt::wide_to_utf8(buffer, str, false);
}
}
template<class StringType>
void TextStream<StringType>::to_string(std::wstring & str, bool clear_string) const
{
if( clear_string )
str.clear();
if constexpr (sizeof(typename StringType::value_type) == sizeof(wchar_t))
{
str.append(buffer);
}
else
{
pt::utf8_to_wide(buffer, str, false);
}
}
template<class StringType>
typename TextStream<StringType>::CharType TextStream<StringType>::operator[](size_t index)
@ -455,6 +511,19 @@ return *this;
}
template<class StringType>
TextStream<StringType> & TextStream<StringType>::operator<<(const TextStream<StringType> & stream)
{
buffer.append(stream.buffer);
return *this;
}
template<class StringType>
template<typename arg_char_type, size_t arg_stack_size, size_t arg_heap_block_size>
TextStream<StringType> & TextStream<StringType>::operator<<(

@ -107,7 +107,8 @@ void ThreadManager::Add(BaseThread * pbase, const wchar_t * thread_name)
data.model_connector.set_winix_session(nullptr);
data.model_connector.set_winix_locale(nullptr); // null for a moment, may will be changed
data.model_connector.set_winix_session_manager(nullptr);// null for a moment, may will be changed
data.model_connector.set_winix_time_zones(nullptr);// null for a moment, may will be changed
data.model_connector.set_winix_time_zones(nullptr); // null for a moment, may will be changed
data.model_connector.set_winix_pattern_cacher(nullptr); // null for a moment, may will be changed
item.object->set_model_connector(&data.model_connector);

@ -67,12 +67,6 @@ void Cat::MakeGet()
}
cur->request->send_as_attachment = cur->request->IsParam(L"attachment");
// IMPROVEME this will be put by a generic method from winix
// if output is html
cur->request->models.Add(L"item", cur->request->item);
////////////////////////////////////////////////////////////
}

@ -47,6 +47,7 @@ FunctionBase::FunctionBase()
template_index = size_t(-1);
need_ssl = false;
need_session = true;
register_default_models = true;
fun.item_content.user_id = -1;
fun.item_content.group_id = -1;

@ -86,6 +86,11 @@ public:
// if false then a temporary session is used
bool need_session;
// true if the function allows to register default models such as 'request', 'item', etc.
// default: true
bool register_default_models;
virtual void Init();
virtual bool HasAccess();
virtual void MakePost();

@ -102,16 +102,6 @@ void Ls::MakeGet()
cur->request->models.Add(L"items", item_tab);
cur->request->models.Add(L"child_dirs", dir_tab);
}
// IMPROVEME this will be put by a generic method from winix
// if output is html
cur->request->models.Add(L"request", cur->request);
if( cur->request->is_item )
{
cur->request->models.Add(L"item", cur->request->item);
}
////////////////////////////////////////////////////////////
}

@ -38,6 +38,7 @@
#include "core/request.h"
#include "templates/templates.h"
#include "core/session.h"
#include "core/log.h"
@ -85,7 +86,10 @@ void Item::fields()
field(L"can_be_removed", &Item::can_be_removed);
field(L"has_read_access", &Item::has_read_access);
field(L"has_write_access", &Item::has_write_access);
field(L"has_read_write_access", &Item::has_read_write_access);
field(L"has_read_exec_access", &Item::has_read_exec_access);
field(L"execute", &Item::execute);
// may we should add a method setTypeFromInt(int t)?
@ -374,17 +378,27 @@ bool Item::can_be_removed() const
}
bool Item::has_read_access()
bool Item::has_read_access() const
{
return item_content.has_read_access();
}
bool Item::has_write_access()
bool Item::has_write_access() const
{
return item_content.has_write_access();
}
bool Item::has_read_write_access() const
{
return item_content.has_read_write_access();
}
bool Item::has_read_exec_access() const
{
return item_content.has_read_exec_access();
}
bool Item::type_is_symlink() const
{
@ -603,10 +617,43 @@ void Item::url_is(EzcEnv & env)
}
//bool Item::has_read_accessxx() const
//{
//
//}
void Item::execute(EzcEnv & env)
{
if( has_read_access() )
{
PatternCacher * pattern_cacher = get_pattern_cacher();
Request * request = get_request();
Config * config = get_config();
if( pattern_cacher && request && config )
{
HtmlTextStream item_run_content;
TemplatesFunctions::EzcGen ezc_generator;
Ezc::Pattern * p = pattern_cacher->GetPattern(*this);
TemplatesFunctions::InitGenerator(ezc_generator, request->models);
ezc_generator.SetPattern(*p);
if( config->allow_ezc_frames_in_executable_items )
ezc_generator.Generate(item_run_content, request->out_streams);
else
ezc_generator.Generate(item_run_content);
ItemContent::print_content(env.out, item_run_content.Str(), item_content.content_raw_type, config->html_filter);
}
}
else
{
Log * log = get_logger();
if( log )
{
(*log) << "Item::execute: permission denied, no read_exec access to item id: " << id << ", url: " << url << ", subject" << subject;
}
}
}

@ -38,12 +38,6 @@
#include <string>
#include "winixmodel.h"
#include "itemcontent.h"
// temporary
#include "core/log.h"
#include "funinfo.h"
#include "templates/htmltextstream.h"
#include "templates/misc.h"
@ -189,8 +183,10 @@ public:
bool can_remove_child(long child_user_id) const;
bool can_be_removed() const;
bool has_read_access();
bool has_write_access();
bool has_read_access() const;
bool has_write_access() const;
bool has_read_write_access() const;
bool has_read_exec_access() const;
bool type_is_symlink() const;
bool type_is_file() const;
@ -202,7 +198,6 @@ public:
bool is_current_dir() const;
protected:
CalcItemsHelper calc_items_by_url(long parent_id, const std::wstring & url);
@ -216,9 +211,8 @@ protected:
void print_dir_without_slash(EzcEnv & env);
void dir_link(EzcEnv & env);
void link(EzcEnv & env);
void url_is(EzcEnv & env);
void execute(EzcEnv & env);
MORM_MEMBER_FIELD(Item)

@ -209,6 +209,17 @@ TimeZones * WinixModel::get_time_zones()
}
PatternCacher * WinixModel::get_pattern_cacher()
{
WinixModelConnector * connector = get_winix_model_connector();
if( connector )
{
return connector->get_winix_pattern_cacher();
}
return nullptr;
}
@ -383,6 +394,19 @@ const TimeZones * WinixModel::get_time_zones() const
}
const PatternCacher * WinixModel::get_pattern_cacher() const
{
const WinixModelConnector * connector = get_winix_model_connector();
if( connector )
{
return connector->get_winix_pattern_cacher();
}
return nullptr;
}

@ -55,7 +55,7 @@ class User;
class Locale;
class SessionManager;
class TimeZones;
class PatternCacher;
class WinixModel : public morm::Model
@ -75,6 +75,7 @@ public:
Locale * get_locale();
SessionManager * get_session_manager();
TimeZones * get_time_zones();
PatternCacher * get_pattern_cacher();
const Config * get_config() const;
const Request * get_request() const;
@ -89,6 +90,7 @@ public:
const Locale * get_locale() const;
const SessionManager * get_session_manager() const;
const TimeZones * get_time_zones() const;
const PatternCacher * get_pattern_cacher() const;

@ -53,6 +53,7 @@ WinixModelConnector::WinixModelConnector()
locale = nullptr;
session_manager = nullptr;
time_zones = nullptr;
pattern_cacher = nullptr;
}
@ -127,6 +128,10 @@ TimeZones * WinixModelConnector::get_winix_time_zones()
return time_zones;
}
PatternCacher * WinixModelConnector::get_winix_pattern_cacher()
{
return pattern_cacher;
}
@ -207,6 +212,12 @@ const TimeZones * WinixModelConnector::get_winix_time_zones() const
}
const PatternCacher * WinixModelConnector::get_winix_pattern_cacher() const
{
return pattern_cacher;
}
void WinixModelConnector::set_winix_config(Config * config)
{
@ -280,6 +291,11 @@ void WinixModelConnector::set_winix_time_zones(TimeZones * time_zones)
}
void WinixModelConnector::set_winix_pattern_cacher(PatternCacher * pattern_cacher)
{
this->pattern_cacher = pattern_cacher;
}

@ -52,6 +52,8 @@ class Session;
class Locale;
class SessionManager;
class TimeZones;
class PatternCacher;
class WinixModelConnector : public morm::ModelConnector
@ -72,6 +74,8 @@ public:
Locale * get_winix_locale();
SessionManager * get_winix_session_manager();
TimeZones * get_winix_time_zones();
PatternCacher * get_winix_pattern_cacher();
const Config * get_winix_config() const;
const Request * get_winix_request() const;
@ -85,6 +89,7 @@ public:
const Locale * get_winix_locale() const;
const SessionManager * get_winix_session_manager() const;
const TimeZones * get_winix_time_zones() const;
const PatternCacher * get_winix_pattern_cacher() const;
void set_winix_config(Config * config);
void set_winix_request(Request * request);
@ -98,6 +103,7 @@ public:
void set_winix_locale(Locale * locale);
void set_winix_session_manager(SessionManager * session_manager);
void set_winix_time_zones(TimeZones * time_zones);
void set_winix_pattern_cacher(PatternCacher * pattern_cacher);
protected:
@ -114,7 +120,7 @@ protected:
Locale * locale;
SessionManager * session_manager;
TimeZones * time_zones;
PatternCacher * pattern_cacher;
};

@ -58,6 +58,13 @@ void HtmlTextStream::clear()
}
void HtmlTextStream::to_str(std::wstring & str)
{
str = TextStream<std::wstring>::Str();
}
/*
without escaping
@ -253,6 +260,11 @@ HtmlTextStream & HtmlTextStream::operator<<(RawText<pt::Date> raw)
}
HtmlTextStream & HtmlTextStream::operator<<(const HtmlTextStream & stream)
{
TextStream<std::wstring>::operator<<(stream.Str());
return *this;
}

@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2010-2015, Tomasz Sowa
* Copyright (c) 2010-2021, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -87,6 +87,7 @@ public:
void Clear();
void clear(); // utf8 methods call clear(), in the future Clear() will be renamed to clear()
void to_str(std::wstring & str);
/*
a helper struct to select a proper operator<<
@ -151,6 +152,9 @@ public:
HtmlTextStream & operator<<(RawText<pt::TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size> > raw);
// this method doesn't escape stream as there is no need for such behavior - stream should be already escaped
HtmlTextStream & operator<<(const HtmlTextStream & stream);
// 'write' don't escapes too
// with these methods you can write a zero character too

@ -280,46 +280,46 @@ void item_dates_equal(Info & i)
void item_run(Info & i)
{
if( !cur->request->is_item )
{
i.out << "<!-- there is no an item to run -->";
return;
}
if( system->HasReadExecAccess(*cur->request->last_item) )
{
Ezc::Pattern * p = pattern_cacher.GetPattern(*cur->request->last_item);
item_run_content.Clear();
InitGenerator(ezc_generator, cur->request->models);
ezc_generator.SetPattern(*p);
if( config->allow_ezc_frames_in_executable_items )
ezc_generator.Generate(item_run_content, cur->request->out_streams);
else
ezc_generator.Generate(item_run_content);
ItemContent::print_content(i.out, item_run_content.Str(), cur->request->last_item->item_content.content_raw_type, config->html_filter);
}
else
{
i.out << "<!-- run: permission denied -->";
}
}
//void item_run(Info & i)
//{
// if( !cur->request->is_item )
// {
// i.out << "<!-- there is no an item to run -->";
// return;
// }
//
// if( system->HasReadExecAccess(*cur->request->last_item) )
// {
// Ezc::Pattern * p = pattern_cacher.GetPattern(*cur->request->last_item);
//
// item_run_content.Clear();
// InitGenerator(ezc_generator, cur->request->models);
// ezc_generator.SetPattern(*p);
//
// if( config->allow_ezc_frames_in_executable_items )
// ezc_generator.Generate(item_run_content, cur->request->out_streams);
// else
// ezc_generator.Generate(item_run_content);
//
// ItemContent::print_content(i.out, item_run_content.Str(), cur->request->last_item->item_content.content_raw_type, config->html_filter);
// }
// else
// {
// i.out << "<!-- run: permission denied -->";
// }
//}
void item_guest_name(Info & i)
{
i.out << cur->request->last_item->item_content.guest_name;
}
//void item_guest_name(Info & i)
//{
// i.out << cur->request->last_item->item_content.guest_name;
//}
void item_html_template(Info & i)
{
i.out << cur->request->last_item->html_template;
}
//void item_html_template(Info & i)
//{
// i.out << cur->request->last_item->html_template;
//}
void item_has_html_template(Info & i)

@ -463,9 +463,9 @@ void Templates::CreateFunctions()
ezc_functions.Insert("item_date_creation_nice", item_date_creation_nice);
ezc_functions.Insert("item_date_modification_nice",item_date_modification_nice);
ezc_functions.Insert("item_dates_equal", item_dates_equal);
ezc_functions.Insert("item_run", item_run);
ezc_functions.Insert("item_guest_name", item_guest_name);
ezc_functions.Insert("item_html_template", item_html_template);
// ezc_functions.Insert("item_run", item_run);
// ezc_functions.Insert("item_guest_name", item_guest_name);
// ezc_functions.Insert("item_html_template", item_html_template);
ezc_functions.Insert("item_has_html_template", item_has_html_template);
// ezc_functions.Insert("item_type_is_dir", item_type_is_dir);
// ezc_functions.Insert("item_type_is_file", item_type_is_file);

@ -353,9 +353,9 @@ namespace TemplatesFunctions
void item_date_creation_nice(Info & i);
void item_date_modification_nice(Info & i);
void item_dates_equal(Info & i);
void item_run(Info & i);
void item_guest_name(Info & i);
void item_html_template(Info & i);
//void item_run(Info & i);
//void item_guest_name(Info & i);
//void item_html_template(Info & i);
void item_has_html_template(Info & i);
//void item_type_is_dir(Info & i);
//void item_type_is_file(Info & i);

Loading…
Cancel
Save