added: privileges (user, groups, permissions)
(not finished yet) classes: User, Group, Users, Groups, UGContainer changed: Dir class into Dirs git-svn-id: svn://ttmath.org/publicrep/cmslu/trunk@467 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
253
core/db.cpp
253
core/db.cpp
@@ -89,6 +89,9 @@ void Db::AssertConnection()
|
||||
else
|
||||
{
|
||||
log << log2 << "ok" << logend;
|
||||
|
||||
if( PQsetClientEncoding(pg_conn, "LATIN2") == -1 )
|
||||
log << log1 << "Db: Can't set the proper client encoding" << logend;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -493,10 +496,10 @@ Error Db::EditItemInItem(Item & item, bool with_subject)
|
||||
{
|
||||
AssertConnection();
|
||||
std::ostringstream query;
|
||||
query << "update core.item set (user_id, group_id, privileges, type, parent_id,";
|
||||
query << "update core.item set (user_id, group_id, privileges, type, parent_id";
|
||||
|
||||
if( with_subject )
|
||||
query << " url";
|
||||
query << ", url";
|
||||
|
||||
query << ") = (";
|
||||
query << '\'' << item.user_id << "', ";
|
||||
@@ -566,19 +569,106 @@ return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Error Db::EditItem(Item & item, bool with_subject)
|
||||
Error Db::EditItemGetId(Item & item)
|
||||
{
|
||||
Error result = EditItemInContent(item);
|
||||
PGresult * r = 0;
|
||||
Error result = Error::ok;
|
||||
|
||||
try
|
||||
{
|
||||
AssertConnection();
|
||||
std::ostringstream query;
|
||||
query << "select item.id, content.id from core.item left join core.content on item.content_id = content.id where item.parent_id='";
|
||||
query << item.parent_id << "' and item.url='" << Escape(item.url) << "';";
|
||||
|
||||
r = AssertQuery(query.str());
|
||||
AssertResultStatus(r, PGRES_TUPLES_OK);
|
||||
|
||||
if( result == Error::ok )
|
||||
result = EditItemInItem(item, with_subject);
|
||||
if( PQntuples(r) != 1 || PQnfields(r) != 2 )
|
||||
throw Error(Error::db_no_item);
|
||||
|
||||
// we cannot use AssertColumn() with a name because both columns are called 'id'
|
||||
item.id = atol( AssertValue(r, 0, 0) );
|
||||
item.content_id = atol( AssertValue(r, 0, 1) );
|
||||
}
|
||||
catch(const Error & e)
|
||||
{
|
||||
result = e;
|
||||
}
|
||||
|
||||
ClearResult(r);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Error Db::EditItemGetContentId(Item & item)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error result = Error::ok;
|
||||
|
||||
try
|
||||
{
|
||||
AssertConnection();
|
||||
std::ostringstream query;
|
||||
query << "select content_id from core.item left join core.content on item.content_id = content.id where item.id='";
|
||||
query << item.id << "';";
|
||||
|
||||
r = AssertQuery(query.str());
|
||||
AssertResultStatus(r, PGRES_TUPLES_OK);
|
||||
|
||||
if( PQntuples(r) != 1 || PQnfields(r) != 1 )
|
||||
throw Error(Error::db_no_item);
|
||||
|
||||
item.content_id = atol( AssertValue(r, 0, 0) );
|
||||
}
|
||||
catch(const Error & e)
|
||||
{
|
||||
result = e;
|
||||
}
|
||||
|
||||
ClearResult(r);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// item.id must be set
|
||||
Error Db::EditItemById(Item & item, bool with_subject)
|
||||
{
|
||||
Error result = EditItemGetContentId(item);
|
||||
|
||||
if( result == Error::ok )
|
||||
{
|
||||
result = EditItemInContent(item);
|
||||
|
||||
if( result == Error::ok )
|
||||
result = EditItemInItem(item, with_subject);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// item.url and item.parent_id must be set
|
||||
Error Db::EditItemByUrl(Item & item, bool with_subject)
|
||||
{
|
||||
Error result = EditItemGetId(item);
|
||||
|
||||
if( result == Error::ok )
|
||||
{
|
||||
result = EditItemInContent(item);
|
||||
|
||||
if( result == Error::ok )
|
||||
result = EditItemInItem(item, with_subject);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
PGresult * Db::GetItemsQuery(Item & item_ref)
|
||||
@@ -670,6 +760,44 @@ void Db::GetItem(std::vector<Item> & item_table, long id)
|
||||
|
||||
|
||||
|
||||
bool Db::GetPriv(Item & item, long id)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
PGresult * r = 0;
|
||||
|
||||
try
|
||||
{
|
||||
AssertConnection();
|
||||
|
||||
std::ostringstream query;
|
||||
query << "select user_id, group_id, privileges, subject, content from core.item left join core.content on item.content_id = content.id where item.id='" << id << "';"; // !! tymczasowo odczytujemy z content i subject
|
||||
|
||||
r = AssertQuery( query.str() );
|
||||
AssertResultStatus(r, PGRES_TUPLES_OK);
|
||||
|
||||
int rows = PQntuples(r);
|
||||
|
||||
if( rows != 1 )
|
||||
throw Error();
|
||||
|
||||
ItemColumns col;
|
||||
col.SetColumns(r);
|
||||
col.SetItem(r, 0, item);
|
||||
|
||||
result = true;
|
||||
}
|
||||
catch(const Error &)
|
||||
{
|
||||
}
|
||||
|
||||
ClearResult(r);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
bool Db::DelItemDelItem(const Item & item)
|
||||
{
|
||||
@@ -831,6 +959,115 @@ void Db::GetDirs(DirContainer & dir_table)
|
||||
|
||||
|
||||
|
||||
void Db::GetUsers(UGContainer<User> & user_table)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
|
||||
try
|
||||
{
|
||||
AssertConnection();
|
||||
|
||||
std::ostringstream query;
|
||||
query << "select id, login, super_user, group_id from core.user left outer join core.group_mem on core.user.id = core.group_mem.user_id order by id asc;";
|
||||
|
||||
r = AssertQuery( query.str() );
|
||||
AssertResultStatus(r, PGRES_TUPLES_OK);
|
||||
|
||||
int rows = PQntuples(r);
|
||||
|
||||
int cid = AssertColumn(r, "id");
|
||||
int cname = AssertColumn(r, "login");
|
||||
int csuper_user = AssertColumn(r, "super_user");
|
||||
int cgroup_id = AssertColumn(r, "group_id");
|
||||
|
||||
User u;
|
||||
long last_id = -1;
|
||||
UGContainer<User>::Iterator iter;
|
||||
|
||||
for(int i = 0 ; i<rows ; ++i)
|
||||
{
|
||||
u.id = atol( AssertValue(r, i, cid) );
|
||||
|
||||
if( u.id != last_id )
|
||||
{
|
||||
u.name = AssertValue(r, i, cname);
|
||||
u.super_user = static_cast<bool>( atoi( AssertValue(r, i, csuper_user) ) );
|
||||
log << log1 << "Db: get user: id:" << u.id << ", name:" << u.name << ", super_user:" << u.super_user << logend;
|
||||
|
||||
iter = user_table.PushBack( u );
|
||||
last_id = u.id;
|
||||
}
|
||||
|
||||
long group_id = atol( AssertValue(r, i, cgroup_id) );
|
||||
|
||||
if( !PQgetisnull(r, i, cgroup_id) && group_id!=-1 && !user_table.Empty() )
|
||||
{
|
||||
iter->groups.push_back(group_id);
|
||||
log << log3 << "Db: user:" << iter->name << " is a member of group_id:" << group_id << logend;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(const Error &)
|
||||
{
|
||||
}
|
||||
|
||||
ClearResult(r);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Db::GetGroups(UGContainer<Group> & group_table)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
|
||||
try
|
||||
{
|
||||
AssertConnection();
|
||||
|
||||
std::ostringstream query;
|
||||
query << "select id, core.group.group, user_id from core.group left outer join core.group_mem on core.group.id = core.group_mem.group_id order by id asc;";
|
||||
|
||||
r = AssertQuery( query.str() );
|
||||
AssertResultStatus(r, PGRES_TUPLES_OK);
|
||||
|
||||
int rows = PQntuples(r);
|
||||
|
||||
int cid = AssertColumn(r, "id");
|
||||
int cname = AssertColumn(r, "group");
|
||||
int cuser_id = AssertColumn(r, "user_id");
|
||||
|
||||
Group g;
|
||||
long last_id = -1;
|
||||
UGContainer<Group>::Iterator iter;
|
||||
|
||||
for(int i = 0 ; i<rows ; ++i)
|
||||
{
|
||||
g.id = atol( AssertValue(r, i, cid) );
|
||||
|
||||
if( g.id != last_id )
|
||||
{
|
||||
g.name = AssertValue(r, i, cname);
|
||||
log << log3 << "Db: get group, id:" << g.id << ", group:" << g.name << logend;
|
||||
|
||||
iter = group_table.PushBack( g );
|
||||
last_id = g.id;
|
||||
}
|
||||
|
||||
long user_id = atol( AssertValue(r, i, cuser_id) );
|
||||
|
||||
if( !PQgetisnull(r, i, cuser_id) && user_id!=-1 && !group_table.Empty() )
|
||||
{
|
||||
iter->members.push_back(user_id);
|
||||
log << log3 << "Db: get group member: user_id:" << user_id << logend;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(const Error &)
|
||||
{
|
||||
}
|
||||
|
||||
ClearResult(r);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user