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,9 @@
#include "core/misc.h"
#include "templates/misc.h"
#include "core/bbcodeparser.h"
#include "core/request.h"
#include "core/users.h"
#include "core/groups.h"
namespace Winix
@@ -83,7 +86,23 @@ void ItemContent::fields()
field(L"meta_admin", meta_admin);
field(L"print_content", &ItemContent::print_content);
field(L"has_static_file", &ItemContent::has_static_file);
field(L"privileges_octal", &ItemContent::privileges_octal);
field(L"user_name", &ItemContent::user_name);
field(L"group_name", &ItemContent::group_name);
field(L"type_is", &ItemContent::type_is);
field(L"is_empty", &ItemContent::is_empty);
field(L"file_type_is_none", &ItemContent::file_type_is_none);
field(L"file_type_is_image", &ItemContent::file_type_is_image);
field(L"file_type_is_video", &ItemContent::file_type_is_video);
field(L"file_type_is_sound", &ItemContent::file_type_is_sound);
field(L"has_thumb", &ItemContent::has_thumb);
// IMPROVEME prepare a setter functions which tests whether content_raw_type_helper and content_parsed_type_helper are correct values
content_raw_type = static_cast<ContentType>(content_raw_type_helper);
content_parsed_type = static_cast<ContentType>(content_parsed_type_helper);
}
@@ -153,66 +172,6 @@ void ItemContent::Clear()
}
/*
* we're using the HtmlFilter only for those contents
*
*/
bool ItemContent::CanContentBeHtmlFiltered(ItemContent::ContentType ct)
{
return ct == ct_text || ct == ct_formatted_text || ct == ct_html || ct == ct_bbcode;
}
bool ItemContent::CanContentBeHtmlFiltered()
{
return CanContentBeHtmlFiltered(content_raw_type);
}
void ItemContent::print_content(HtmlTextStream & out, const std::wstring & content, ItemContent::ContentType content_type, bool is_html_filter_on)
{
using TemplatesFunctions::R;
if( is_html_filter_on && !ItemContent::CanContentBeHtmlFiltered(content_type) )
out << R("<nofilter>");
if( content_type == ItemContent::ct_text )
{
out << content;
}
else
if( content_type == ItemContent::ct_formatted_text )
{
TemplatesFunctions::HtmlEscapeFormTxt(out, content);
}
else
if( content_type == ItemContent::ct_bbcode )
{
static std::wstring out_temp;
out_temp.clear();
out_temp.reserve(content.size()*2);
BBCODEParser bbcode_parser; // IMPROVE ME move me to a better place
bbcode_parser.Filter(content.c_str(), out_temp);
out << R(out_temp);
}
else
{
// ct_html, ct_other
out << R(content);
}
if( is_html_filter_on && !ItemContent::CanContentBeHtmlFiltered(content_type) )
out << R("</nofilter>");
}
void ItemContent::print_content(Ezc::FunInfo<HtmlTextStream> & env)
{
print_content(env.out, content_raw, content_raw_type, true); // IMPROVE ME get the 'true' from the config (config->html_filter)
}
bool ItemContent::do_migration(int & current_table_version)
{
@@ -305,6 +264,268 @@ bool ItemContent::do_migration_to_3()
}
bool ItemContent::has_access(const User * current_user, int mask) const
{
if( current_user )
{
if( current_user->super_user )
{
// super user is allowed everything
return true;
}
if( user_id != -1 && current_user->id == user_id )
{
// the owner
return ((privileges >> 9) & mask) == mask;
}
if( group_id != -1 && current_user->IsMemberOf(group_id) )
{
// group
return ((privileges >> 6) & mask) == mask;
}
// others -- others logged users
return ((privileges >> 3) & mask) == mask;
}
// guests -- not logged users
return (privileges & mask) == mask;
}
bool ItemContent::has_read_access(const User * current_user) const
{
return has_access(current_user, 4); // r
}
bool ItemContent::has_write_access(const User * current_user) const
{
return has_access(current_user, 2); // w
}
bool ItemContent::has_read_write_access(const User * current_user) const
{
return has_access(current_user, 6); // r+w
}
bool ItemContent::has_read_exec_access(const User * current_user) const
{
if( current_user && current_user->super_user )
{
// there must be at least one 'x' (for the root)
// !! CHECK ME: is it applicable to directories too?
return (privileges & 01111) != 0;
}
return has_access(current_user, 5); // r+x
}
bool ItemContent::has_read_access() const
{
return has_read_access(get_current_user());
}
bool ItemContent::has_write_access() const
{
return has_write_access(get_current_user());
}
bool ItemContent::has_read_write_access() const
{
return has_read_write_access(get_current_user());
}
bool ItemContent::has_read_exec_access() const
{
return has_read_exec_access(get_current_user());
}
bool ItemContent::is_sticky_bit_set() const
{
int mask = 010000;
return (privileges & mask) == mask;
}
/*
* we're using the HtmlFilter only for those contents
*
*/
bool ItemContent::CanContentBeHtmlFiltered(ItemContent::ContentType ct)
{
return ct == ct_text || ct == ct_formatted_text || ct == ct_html || ct == ct_bbcode;
}
bool ItemContent::CanContentBeHtmlFiltered()
{
return CanContentBeHtmlFiltered(content_raw_type);
}
void ItemContent::print_content(HtmlTextStream & out, const std::wstring & content, ItemContent::ContentType content_type, bool is_html_filter_on)
{
using TemplatesFunctions::R;
if( is_html_filter_on && !ItemContent::CanContentBeHtmlFiltered(content_type) )
out << R("<nofilter>");
if( content_type == ItemContent::ct_text )
{
out << content;
}
else
if( content_type == ItemContent::ct_formatted_text )
{
TemplatesFunctions::HtmlEscapeFormTxt(out, content);
}
else
if( content_type == ItemContent::ct_bbcode )
{
static std::wstring out_temp;
out_temp.clear();
out_temp.reserve(content.size()*2);
BBCODEParser bbcode_parser; // IMPROVE ME move me to a better place
bbcode_parser.Filter(content.c_str(), out_temp);
out << R(out_temp);
}
else
{
// ct_html, ct_other
out << R(content);
}
if( is_html_filter_on && !ItemContent::CanContentBeHtmlFiltered(content_type) )
out << R("</nofilter>");
}
void ItemContent::print_content(EzcEnv & env)
{
print_content(env.out, content_raw, content_raw_type, true); // IMPROVE ME get the 'true' from the config (config->html_filter)
}
void ItemContent::has_static_file(EzcEnv & env)
{
env.res = file_type != WINIX_ITEM_FILETYPE_NONE && !file_path.empty();
}
void ItemContent::privileges_octal(EzcEnv & env)
{
env.out << Toa(privileges, 8);
}
void ItemContent::user_name(EzcEnv & env)
{
Users * users = get_users();
if( users )
{
User * puser = users->GetUser(user_id);
TemplatesFunctions::print_user_name(env, puser, guest_name);
}
}
void ItemContent::group_name(EzcEnv & env)
{
Groups * groups = get_groups();
if( groups )
{
Group * pgroup = groups->GetGroup(group_id);
if( pgroup )
env.out << pgroup->name;
else
env.out << group_id;
}
}
void ItemContent::type_is(EzcEnv & env)
{
if( content_raw_type == ItemContent::ct_text && env.par == L"text" )
env.res = true;
else
if( content_raw_type == ItemContent::ct_formatted_text && env.par == L"formatted text" )
env.res = true;
else
if( content_raw_type == ItemContent::ct_html && env.par == L"html" )
env.res = true;
else
if( content_raw_type == ItemContent::ct_bbcode && env.par == L"bbcode" )
env.res = true;
else
if( content_raw_type == ItemContent::ct_other && env.par == L"other" )
env.res = true;
}
void ItemContent::is_empty(EzcEnv & env)
{
env.res = content_raw.empty();
}
void ItemContent::file_type_is_none(EzcEnv & env)
{
env.res = file_type == WINIX_ITEM_FILETYPE_NONE;
}
/*
* when we change file_type to string the we can return true only for
* those images which can be able to show by a webbrowser
*
*/
void ItemContent::file_type_is_image(EzcEnv & env)
{
env.res = file_type == WINIX_ITEM_FILETYPE_IMAGE;
}
/*
* similar we can return true only for those videos which can be rendered by a webbrowser
*/
void ItemContent::file_type_is_video(EzcEnv & env)
{
env.res = file_type == WINIX_ITEM_FILETYPE_VIDEO;
}
void ItemContent::file_type_is_sound(EzcEnv & env)
{
//env.res = file_type == WINIX_ITEM_FILETYPE_;
env.res = false;
}
void ItemContent::has_thumb(EzcEnv & env)
{
env.res = file_has_thumb;
}
} // namespace Winix