some ezc functions from templates/item.cpp moved to Item and ItemContent

methods HasAccess() HasReadAccess() and similar moved from System to Item and ItemContent
This commit is contained in:
2021-06-18 19:18:13 +02:00
parent ebd791a256
commit ec94dff7d7
32 changed files with 1255 additions and 544 deletions

View File

@@ -37,6 +37,8 @@
#include "finder.h"
#include "core/request.h"
#include "templates/templates.h"
#include "core/session.h"
namespace Winix
@@ -58,21 +60,32 @@ void Item::fields()
int type_helper = static_cast<int>(type);
field(L"id", id, morm::FT::no_insertable | morm::FT::no_updatable | morm::FT::primary_key);
field(L"parent_id", parent_id);
field(L"type", type_helper);
field(L"url", url);
field(L"subject", subject);
field(L"template", html_template);
field(L"sort_index", sort_index);
field(L"id", id, morm::FT::no_insertable | morm::FT::no_updatable | morm::FT::primary_key);
field(L"parent_id", parent_id);
field(L"type", type_helper);
field(L"url", url);
field(L"subject", subject);
field(L"template", html_template);
field(L"sort_index", sort_index);
field(L"content_id", L"content", item_content, morm::FT::foreign_key);
field(L"is", &Item::is);
field(L"dir_link", &Item::dir_link);
field(L"link", &Item::link);
field(L"dir_link", &Item::dir_link);
field(L"link", &Item::link);
field(L"is_parent_for_current_dir", &Item::is_parent_for_current_dir);
field(L"is_current_dir", &Item::is_current_dir);
field(L"is_root", &Item::is_root);
field(L"is_current_dir", &Item::is_current_dir);
field(L"is_root_dir", &Item::is_root_dir);
field(L"type_is_symlink", &Item::type_is_symlink);
field(L"type_is_file", &Item::type_is_file);
field(L"type_is_dir", &Item::type_is_dir);
field(L"type_is_none", &Item::type_is_none);
field(L"url_is", &Item::url_is);
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);
// may we should add a method setTypeFromInt(int t)?
@@ -156,8 +169,6 @@ bool Item::update(morm::ModelData * model_data, bool update_whole_tree)
}
void Item::Clear()
{
id = -1;
@@ -171,199 +182,6 @@ void Item::Clear()
}
CalcItemsHelper Item::calc_items_by_url(long parent_id, const std::wstring & url)
{
morm::Finder<CalcItemsHelper> finder(model_connector);
CalcItemsHelper helper = finder.
prepare_to_select().
raw("select count(id) as size, min(id) as item_id from core.item").
where().
eq(L"parent_id", parent_id).
eq(L"url", url).
get();
return helper;
}
/*
* may we should check for '.' and '..' here too?
*/
bool Item::prepare_url()
{
std::wstring temp_url;
bool is_that_url;
const int max_index = 99;
size_t index = 1;
std::wstring postfix;
// only root dir may not have the url
if( parent_id != -1 && url.empty() )
url = L"_";
do
{
if( index > 1 )
{
postfix = L"_(";
pt::Toa(index, postfix, false);
postfix += L")";
}
PrepareNewFileName(url, postfix, temp_url);
CalcItemsHelper helper = calc_items_by_url(parent_id, temp_url);
if( helper.size > 0 )
{
is_that_url = true;
}
else
{
url = temp_url;
is_that_url = false;
}
index += 1;
}
while( is_that_url && index <= max_index );
return !is_that_url;
}
void Item::propagate_connector()
{
item_content.set_connector(model_connector);
}
// IMPROVEME move me to a better place
void Item::print_dir(Ezc::FunInfo<HtmlTextStream> & env)
{
Request * req = get_request();
if( req )
{
for(size_t a=0 ; a < req->dir_tab.size() ; ++a)
env.out << req->dir_tab[a]->url << '/';
}
}
// IMPROVEME move me to a better place
void Item::print_dir_without_slash(Ezc::FunInfo<HtmlTextStream> & env)
{
Request * req = get_request();
if( req )
{
for(size_t a=0 ; a < req->dir_tab.size() ; ++a)
{
env.out << req->dir_tab[a]->url;
if( a < req->dir_tab.size()-1 )
env.out << '/';
}
}
}
void Item::is(Ezc::FunInfo<HtmlTextStream> & env)
{
Request * req = get_request();
if( req )
{
env.res = req->is_item;
}
}
void Item::dir_link(Ezc::FunInfo<HtmlTextStream> & env)
{
Dirs * dirs = get_dirs();
std::vector<Item*> dir_tab;
if( dirs && dirs->CreateDirTab(parent_id, dir_tab) )
{
for(Item * pitem : dir_tab)
{
env.out << pitem->url << '/';
}
}
}
void Item::link(Ezc::FunInfo<HtmlTextStream> & env)
{
Config * config = get_config();
Request * req = get_request();
if( config && req )
{
TemplatesFunctions::doc_proto(env);
if( !req->subdomain.empty() )
env.out << req->subdomain << '.';
env.out << config->base_url;
if( parent_id == req->dir_tab.back()->id )
{
print_dir(env);
}
else
{
dir_link(env);
}
env.out << url;
}
}
void Item::is_parent_for_current_dir(Ezc::FunInfo<HtmlTextStream> & env)
{
Request * req = get_request();
if( req )
{
if( req->dir_tab.size() > 1 )
{
env.res = (id == req->dir_tab[req->dir_tab.size() - 2]->id);
}
}
}
void Item::is_current_dir(Ezc::FunInfo<HtmlTextStream> & env)
{
Request * req = get_request();
if( req )
{
env.res = (id == req->dir_tab.back()->id);
}
}
// rename to is_root_dir
void Item::is_root(Ezc::FunInfo<HtmlTextStream> & env)
{
Request * req = get_request();
if( req )
{
// add a test whether this is a directory (not a file or a symlink)
env.res = (parent_id == -1);
}
}
@@ -499,5 +317,295 @@ bool Item::do_migration_to_4()
bool Item::can_remove_child(const User * current_user, long child_user_id) const
{
bool res = false;
if( type == Type::dir )
{
if( current_user && current_user->super_user )
{
res = true;
}
else
{
res = item_content.has_write_access(current_user);
if( res && item_content.is_sticky_bit_set() )
{
// IMPROVEME what about if child_user_id is -1 (guest)
res = (item_content.user_id == child_user_id);
}
}
}
return res;
}
bool Item::can_be_removed(const User * current_user) const
{
bool res = false;
const Dirs * dirs = get_dirs();
if( dirs && parent_id != -1 )
{
const Item * parent_dir = dirs->GetDir(parent_id);
if( parent_dir )
{
res = parent_dir->can_remove_child(current_user, id);
}
}
return res;
}
bool Item::can_remove_child(long child_user_id) const
{
return can_remove_child(get_current_user(), child_user_id);
}
bool Item::can_be_removed() const
{
return can_be_removed(get_current_user());
}
bool Item::has_read_access()
{
return item_content.has_read_access();
}
bool Item::has_write_access()
{
return item_content.has_write_access();
}
bool Item::type_is_symlink() const
{
return (type == Type::symlink);
}
bool Item::type_is_file() const
{
return (type == Type::file);
}
bool Item::type_is_dir() const
{
return (type == Type::dir);
}
bool Item::type_is_none() const
{
return (type == Type::none);
}
bool Item::is_root_dir() const
{
return (type == Type::dir && parent_id == -1);
}
CalcItemsHelper Item::calc_items_by_url(long parent_id, const std::wstring & url)
{
morm::Finder<CalcItemsHelper> finder(model_connector);
CalcItemsHelper helper = finder.
prepare_to_select().
raw("select count(id) as size, min(id) as item_id from core.item").
where().
eq(L"parent_id", parent_id).
eq(L"url", url).
get();
return helper;
}
/*
* may we should check for '.' and '..' here too?
*/
bool Item::prepare_url()
{
std::wstring temp_url;
bool is_that_url;
const int max_index = 99;
size_t index = 1;
std::wstring postfix;
// only root dir may not have the url
if( parent_id != -1 && url.empty() )
url = L"_";
do
{
if( index > 1 )
{
postfix = L"_(";
pt::Toa(index, postfix, false);
postfix += L")";
}
PrepareNewFileName(url, postfix, temp_url);
CalcItemsHelper helper = calc_items_by_url(parent_id, temp_url);
if( helper.size > 0 )
{
is_that_url = true;
}
else
{
url = temp_url;
is_that_url = false;
}
index += 1;
}
while( is_that_url && index <= max_index );
return !is_that_url;
}
void Item::propagate_connector()
{
item_content.set_connector(model_connector);
}
// IMPROVEME move me to a better place
void Item::print_dir(EzcEnv & env)
{
Request * req = get_request();
if( req )
{
for(size_t a=0 ; a < req->dir_tab.size() ; ++a)
env.out << req->dir_tab[a]->url << '/';
}
}
// IMPROVEME move me to a better place
void Item::print_dir_without_slash(EzcEnv & env)
{
Request * req = get_request();
if( req )
{
for(size_t a=0 ; a < req->dir_tab.size() ; ++a)
{
env.out << req->dir_tab[a]->url;
if( a < req->dir_tab.size()-1 )
env.out << '/';
}
}
}
void Item::dir_link(EzcEnv & env)
{
Dirs * dirs = get_dirs();
std::vector<Item*> dir_tab;
if( dirs && dirs->CreateDirTab(parent_id, dir_tab) )
{
for(Item * pitem : dir_tab)
{
env.out << pitem->url << '/';
}
}
}
void Item::link(EzcEnv & env)
{
Config * config = get_config();
Request * req = get_request();
if( config && req )
{
TemplatesFunctions::doc_proto(env);
if( !req->subdomain.empty() )
env.out << req->subdomain << '.';
env.out << config->base_url;
if( parent_id == req->dir_tab.back()->id )
{
print_dir(env);
}
else
{
dir_link(env);
}
env.out << url;
}
}
bool Item::is_parent_for_current_dir() const
{
bool res = false;
const Request * req = get_request();
if( req )
{
if( req->dir_tab.size() > 1 )
{
res = (id == req->dir_tab[req->dir_tab.size() - 2]->id);
}
}
return res;
}
bool Item::is_current_dir() const
{
bool res = false;
const Request * req = get_request();
if( req )
{
res = (id == req->dir_tab.back()->id);
}
return res;
}
void Item::url_is(EzcEnv & env)
{
env.res = (url == env.par);
}
} // namespace Winix