added: functions: chmod, chown
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@587 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
193
content/priv.cpp
193
content/priv.cpp
@@ -2,7 +2,7 @@
|
||||
* This file is a part of CMSLU -- Content Management System like Unix
|
||||
* and is not publicly distributed
|
||||
*
|
||||
* Copyright (c) 2008-2009, Tomasz Sowa
|
||||
* Copyright (c) 2008-2010, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
@@ -14,6 +14,10 @@
|
||||
#include "../core/data.h"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
bool Content::PrivCheckAccess()
|
||||
{
|
||||
// we do not check permissions here
|
||||
@@ -32,61 +36,135 @@ return true;
|
||||
|
||||
|
||||
|
||||
|
||||
void Content::FunPriv(Item & item, long user_id, long group_id, int privileges)
|
||||
bool Content::ChangeOwner(Item & item, long user_id, long group_id)
|
||||
{
|
||||
if( user_id==item.user_id && group_id==item.group_id && privileges==item.privileges )
|
||||
return;
|
||||
|
||||
if( !request.CanChangeUser(item, user_id) )
|
||||
if( user_id!=item.user_id || group_id!=item.group_id )
|
||||
{
|
||||
log << log3 << "Content: can't change the user" << logend;
|
||||
return;
|
||||
}
|
||||
|
||||
if( !request.CanChangeGroup(item, group_id) )
|
||||
{
|
||||
log << log3 << "Content: can't change the group" << logend;
|
||||
return;
|
||||
if( !request.CanChangeUser(item, user_id) )
|
||||
{
|
||||
log << log3 << "Content: can't change the user" << logend;
|
||||
return false;
|
||||
}
|
||||
|
||||
if( !request.CanChangeGroup(item, group_id) )
|
||||
{
|
||||
log << log3 << "Content: can't change the group" << logend;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if( !request.CanChangePrivileges(item, privileges) )
|
||||
item.user_id = user_id;
|
||||
item.group_id = group_id;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Content::ChangePrivileges(Item & item, int privileges)
|
||||
{
|
||||
if( privileges != item.privileges )
|
||||
{
|
||||
log << log3 << "Content: can't change privileges" << logend;
|
||||
return;
|
||||
if( !request.CanChangePrivileges(item, privileges) )
|
||||
{
|
||||
log << log3 << "Content: can't change privileges" << logend;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
item.user_id = user_id;
|
||||
item.group_id = group_id;
|
||||
item.privileges = privileges;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Content::ChangePriv(Item & item, long user_id, long group_id, int privileges)
|
||||
{
|
||||
if( change_owner )
|
||||
{
|
||||
if( !ChangeOwner(item, user_id, group_id) )
|
||||
return;
|
||||
}
|
||||
|
||||
if( change_priv )
|
||||
{
|
||||
if( !ChangePrivileges(item, privileges) )
|
||||
return;
|
||||
}
|
||||
|
||||
request.session->done_status = db.EditPrivById(item, item.id);
|
||||
}
|
||||
|
||||
|
||||
void Content::PrivLog(const char * what, const std::string & url, long user, long group, int priv)
|
||||
|
||||
void Content::PrivLogStart(const char * what, long user, long group, int priv)
|
||||
{
|
||||
log << log3 << "Content: "
|
||||
<< what << url
|
||||
<< ", user: " << user
|
||||
<< ", group: " << group
|
||||
<< ", priv: " << priv
|
||||
<< logend;
|
||||
log << log2 << what;
|
||||
|
||||
if( change_owner )
|
||||
{
|
||||
User * puser = data.users.GetUser(user);
|
||||
Group * pgroup = data.groups.GetGroup(group);
|
||||
|
||||
log << "new user: ";
|
||||
|
||||
if( puser )
|
||||
log << puser->name;
|
||||
else
|
||||
log << "id: " << user;
|
||||
|
||||
log << ", new group: ";
|
||||
|
||||
if( pgroup )
|
||||
log << pgroup->name;
|
||||
else
|
||||
log << "id: " << group;
|
||||
|
||||
if( change_priv )
|
||||
log << ", ";
|
||||
}
|
||||
|
||||
if( change_priv )
|
||||
{
|
||||
char buf[30];
|
||||
sprintf(buf, "0%o", priv);
|
||||
log << "privileges: " << buf;
|
||||
}
|
||||
|
||||
log << logend;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Content::PrivLog(const char * what, long id, const std::string & url)
|
||||
{
|
||||
Item * root = 0;
|
||||
|
||||
if( id != -1 )
|
||||
root = data.dirs.GetRootDir();
|
||||
|
||||
log << log3 << "Content: " << what;
|
||||
|
||||
if( root && root->id == id )
|
||||
log << "(root)";
|
||||
else
|
||||
log << url;
|
||||
|
||||
log << logend;
|
||||
}
|
||||
|
||||
|
||||
void Content::PrivFilesInDir(long parent_id)
|
||||
{
|
||||
request.item_table.clear();
|
||||
db.GetItems(request.item_table, parent_id, Item::file, false, false, true);
|
||||
|
||||
std::vector<Item>::iterator i = request.item_table.begin();
|
||||
|
||||
for( ; i != request.item_table.end() ; ++i)
|
||||
{
|
||||
PrivLog("changed file: ", i->url, user_id_file, group_id_file, priv_file);
|
||||
FunPriv(*i, user_id_file, group_id_file, priv_file);
|
||||
PrivLog("changed file: ", -1, i->url);
|
||||
ChangePriv(*i, user_id_file, group_id_file, priv_file);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,9 +179,11 @@ void Content::PrivDir(long parent_id)
|
||||
|
||||
for( ; i != data.dirs.ParentEnd() ; i = data.dirs.NextParent(i) )
|
||||
{
|
||||
PrivLog("changed dir: ", i->second->url, user_id_dir, group_id_dir, priv_dir);
|
||||
FunPriv(*(i->second), user_id_dir, group_id_dir, priv_dir);
|
||||
PrivDir(i->second->id);
|
||||
PrivLog("changed dir: ", -1, i->second->url);
|
||||
ChangePriv(*(i->second), user_id_dir, group_id_dir, priv_dir);
|
||||
|
||||
if( subdirectories )
|
||||
PrivDir(i->second->id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,15 +195,26 @@ bool Content::ReadPriv(const char * user_in, const char * group_in, const char *
|
||||
std::string * group_str = request.PostVar(group_in);
|
||||
std::string * priv_str = request.PostVar(priv_in);
|
||||
|
||||
if( !user_str || !group_str || !priv_str )
|
||||
if( change_owner && (!user_str || !group_str) )
|
||||
{
|
||||
log << log1 << "Content: PostFunPriv: there is no some post variables" << logend;
|
||||
log << log1 << "Content: PostFunPriv: there is no some post variables for changing the owner" << logend;
|
||||
return false;
|
||||
}
|
||||
|
||||
user_id = data.users.GetUserId( *user_str );
|
||||
group_id = data.groups.GetGroupId( *group_str );
|
||||
priv = strtol( priv_str->c_str() , 0, 8);
|
||||
if( change_priv && !priv_str )
|
||||
{
|
||||
log << log1 << "Content: PostFunPriv: there is no some post variables for changing privileges" << logend;
|
||||
return false;
|
||||
}
|
||||
|
||||
if( change_owner )
|
||||
{
|
||||
user_id = data.users.GetUserId( *user_str );
|
||||
group_id = data.groups.GetGroupId( *group_str );
|
||||
}
|
||||
|
||||
if( change_priv )
|
||||
priv = strtol( priv_str->c_str() , 0, 8);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -137,13 +228,21 @@ void Content::PrivDir()
|
||||
if( !ReadPriv("userdir", "groupdir", "privilegesdir", user_id_dir, group_id_dir, priv_dir) )
|
||||
return;
|
||||
|
||||
PrivLogStart("Content: changes for files: ", user_id_file, group_id_file, priv_file);
|
||||
PrivLogStart("Content: changes for dirs: ", user_id_dir, group_id_dir, priv_dir);
|
||||
|
||||
|
||||
if( request.IsPostVar("changecurrentdir") )
|
||||
{
|
||||
PrivLog("changed dir: ", request.dir_table.back()->url, user_id_dir, group_id_dir, priv_dir);
|
||||
FunPriv(*request.dir_table.back(), user_id_dir, group_id_dir, priv_dir);
|
||||
Item & last_dir = *request.dir_table.back();
|
||||
PrivLog("changed dir: ", last_dir.id, last_dir.url);
|
||||
ChangePriv(*request.dir_table.back(), user_id_dir, group_id_dir, priv_dir);
|
||||
}
|
||||
|
||||
// go through all directories (recurrence)
|
||||
|
||||
subdirectories = request.IsPostVar("changesubdirs");
|
||||
|
||||
// go through all directories
|
||||
PrivDir(request.dir_table.back()->id);
|
||||
|
||||
RedirectToLastDir();
|
||||
@@ -157,14 +256,16 @@ void Content::PrivOneItem()
|
||||
if( !ReadPriv("user", "group", "privileges", user_id_file, group_id_file, priv_file) )
|
||||
return;
|
||||
|
||||
PrivLogStart("Content: changes: ", user_id_file, group_id_file, priv_file);
|
||||
|
||||
if( request.is_item )
|
||||
{
|
||||
FunPriv(request.item, user_id_file, group_id_file, priv_file);
|
||||
ChangePriv(request.item, user_id_file, group_id_file, priv_file);
|
||||
RedirectTo(request.item);
|
||||
}
|
||||
else
|
||||
{
|
||||
FunPriv(*request.dir_table.back(), user_id_file, group_id_file, priv_file);
|
||||
ChangePriv(*request.dir_table.back(), user_id_file, group_id_file, priv_file);
|
||||
RedirectToLastDir();
|
||||
}
|
||||
}
|
||||
@@ -176,6 +277,8 @@ void Content::PostFunPriv()
|
||||
if( !PrivCheckAccess() )
|
||||
return;
|
||||
|
||||
change_owner = (request.pfunction->code == FUN_PRIV || request.pfunction->code == FUN_CHOWN);
|
||||
change_priv = (request.pfunction->code == FUN_PRIV || request.pfunction->code == FUN_CHMOD);
|
||||
|
||||
if( request.IsParam("r") )
|
||||
{
|
||||
@@ -185,6 +288,8 @@ void Content::PostFunPriv()
|
||||
{
|
||||
PrivOneItem();
|
||||
}
|
||||
|
||||
data.dirs.CheckRootDir();
|
||||
}
|
||||
|
||||
|
||||
@@ -193,3 +298,5 @@ void Content::FunPriv()
|
||||
{
|
||||
PrivCheckAccess();
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user