skip to main content

kiesler.at
You are NOT logged in. ( login | New Account Signup | Forgot Your Password? ) Why log in?
View Topic
Forums > Content Management > phpWebSite > View Restriction for Documents


The ad will go away if you log in.

<< [ 1 ] >>

| NewTopic New Topic | Reply Reply

 

rck “View Restriction for Documents” #1
Admin[1999] from Korneuburg 2006-08-21 00:24

So the Most Wanted Hack contest ended up in showing up a need for better document rights. That's why I've hacked the documents module so you could restrict documents to a certain group on a per-document basis.

What to do? First, add a database attribute:

[code]alter table mod_documents_docs add restrict_view int after full_text;[/code]

The rest isn't too hard either -- the documents module is written very nicely and modular. We just need to change the DocumentManager class to watch the rights, the Document class to provide a front-end for us and the corresponding template so that front-end is template-able.

In class/DocumentManager.php, add this:

[code]
function fetchViewRestrictions($id) {

$sql = "SELECT restrict_view ".
"FROM {$GLOBALS['core']->tbl_prefix}mod_documents_docs ".
"WHERE id='$id'";

$restrictions = $GLOBALS['core']->quickFetch($sql);

return($restrictions['restrict_view']);
}




function isAllowedToDo() {

return( ($_SESSION['OBJ_user']->isUser() or !JAS_USE_USER_RIGHTS) or
($this->settings['userview'])
);

}


function isViewRestricted() {

$view_restrictions=$this->fetchViewRestrictions($_REQUEST['JAS_Document_id']);

if( !empty($view_restrictions) and
$view_restrictions>0
)
$view_restriction=!($_SESSION['OBJ_user']->userInGroup($view_restrictions));
else
$view_restriction=false;

return($view_restriction);
}



function mayViewDocument() {

$can_do= is_numeric($_REQUEST['JAS_Document_id']) and
$_SESSION['OBJ_user']->allow_access('document', 'view_document');

$allowed_to_do = $this->isAllowedToDo();

$view_restriction = $this->isViewRestricted();


return( $_SESSION['OBJ_user']->isDeity() or
( ($can_do or $allowed_to_do) and !$view_restriction )
);

}



function mayViewFile() {
$can_do= is_numeric($_REQUEST['JAS_File_id']) and
$_SESSION['OBJ_user']->allow_access('document', 'view_document');

$allowed_to_do=$this->isAllowedToDo();

$view_restriction = $this->isViewRestricted();

return( $_SESSION['OBJ_user']->isDeity() or
( ($can_do or $allowed_to_do) and !$view_restriction )
);
}
[/code]

these new functions replace the existing right-checks of _view and _download. So, instead of the existing _view and _download functions in DocumentManager, use these two:

[code] function _view() {

if($this->mayViewDocument()) {
$this->document = new JAS_Document($_REQUEST['JAS_Document_id']);
$_REQUEST['JAS_Document_op'] = "view";
} else {

$view_restrictions=$this->fetchViewRestrictions($_REQUEST['JAS_Document_id']);

if($view_restrictions > 0)
$this->message = $_SESSION['translate']->it("Sorry! This document is restricted to group [var1]", $view_restrictions);
else
$this->message = $_SESSION['translate']->it('You do not have permission to [var1] documents', $_SESSION['translate']->it("view"));

$this->_list();
}

}


function _download() {

if($this->mayViewFile()) {

if(!isset($this->document))
$this->document = new JAS_Document();

$_REQUEST['JAS_Document_op'] = "downloadFile";
$this->document->action();
return;
} else {

$view_restrictions=$this->fetchViewRestrictions($_REQUEST['JAS_Document_id']);

if($view_restrictions > 0)
$this->message = $_SESSION['translate']->it("Sorry! This document is restricted to group [var1]", $view_restrictions);
else
$this->message = $_SESSION['translate']->it('You do not have permission to [var1] documents', $_SESSION['translate']->it("view"));

$this->_list();
}
}
[/code]

With the changes until now, the rights-management is already in effect. You'd just need to change the "restrict_view" attribute of a given document-record to read the group-id you'd like to restrict a document to.

Let's make a front-end for that, too. Open class/Document.php and add

[code] var $restrict_view = NULL;[/code]

after var $list = NULL. Next, put the following getter / setter methods for the new restrict_view variable somewhere into Document.php:

[code] function getRestrictView() {
return($this->restrict_view);
}


function setRestrictView($restrict_view) {
$this->restrict_view = $restrict_view;
}

[/code]

Then, in _edit, after FormTags['NAME'], put:

[code] $restrict_view = $this->getRestrictView();
$FormTags['RESTRICTVIEW_LABEL']=$_SESSION['translate']->it('Restrict view to group...');
$FormTags['RESTRICTVIEW'] = "";
[/code]

and in _save, just before the end of custom save comment, put

[code] $this->setRestrictView( $_REQUEST['JAS_Document_restrictview'] );
[/code]

To finish the whole thing, add this to templates/forms/form_layout.php:

[code]
{RESTRICTVIEW_LABEL}
{RESTRICTVIEW}


[/code]

Enjoy!
---
200 channels and... nothing but cats.
Quote Quote
singletrack “Re: View Restriction for Documents” #2
Activator[18] 2006-08-21 06:16

The Manager class that Documents uses already has a group rights field that might have been useful in this.

Have a look at /core/Manager.php and the getItems() function which lets you filter out items that don't belong to the user's group (or groups).
Quote Quote
rck “Re: View Restriction for Documents” #3
Admin[1999] from Korneuburg 2006-08-21 22:03

Sounds Cool, but I didn't find it! Just looked into the install-sql file again, and still can't:

[code]$ cat install.sql
CREATE TABLE mod_documents_docs (
id int(10) NOT NULL default '0',
owner varchar(20) binary default '',
editor varchar(20) binary default '',
ip varchar(255) NULL,
created int(11) NOT NULL default '0',
updated int(11) NOT NULL default '0',
hidden int(1) NOT NULL default '1',
approved int(1) NOT NULL default '0',
label varchar(255) NOT NULL default '',
PRIMARY KEY (id)
);

CREATE TABLE mod_documents_files (
id int(10) NOT NULL default '0',
owner varchar(20) binary default '',
ip varchar(255) NULL,
created int(11) NOT NULL default '0',
doc int(10) NOT NULL default '0',
name varchar(255) NOT NULL default '',
size varchar(255) NOT NULL default '',
type varchar(255) NOT NULL default '',
PRIMARY KEY (id)
);

CREATE TABLE mod_documents_settings (
userview int(1) NOT NULL default '0',
userdownload int(1) NOT NULL default '0',
showblock int(1) NOT NULL default '1',
approval int(1) NOT NULL default '1',
index (userview, userdownload, showblock)
);

INSERT INTO mod_documents_settings VALUES ('1', '1', '0', '0');
[/code]
---
200 channels and... nothing but cats.
Quote Quote
singletrack “Re: View Restriction for Documents” #4
Activator[18] 2006-09-02 16:00

It's part of the Item and Manager classes that Documents EXTENDS. Just add a groups field to the database then use the addGroup/removeGroup functions plus the filterGroups parameter in getItems() in Manager.

It's not in the Document module. It's in the core classes.

One caveat, for some reason the groups member variable in Item is commented out in 0.10.2. Not sure why yet.
Quote Quote
rck “Re: View Restriction for Documents” #5
Admin[1999] from Korneuburg 2006-09-05 09:17

singletrack Not sure why yet.


Thanks. Please keep me updated...
---
200 channels and... nothing but cats.
Quote Quote
mediagirilla “Re: View Restriction for Documents” #6
Member[1] 2007-06-22 02:56

O.K.,
I love this idea! I made the changes listed, but I must have done something wrong, becausealthough the user groups field shows up in the control panel, it works only as a text box and does not list the user groups.

I've looked over the list of instructions and checked it twice, but I can't find my mistake?

Any ideas?
Quote Quote
dlmeckes “Re: View Restriction for Documents” #7
Member[4] 2007-12-15 14:59

I'm getting a

unexpected T_STRING in /path-to-my-site/mod/documents/class/DocumentManager.php on line 261

message
Quote Quote
dlmeckes “Re: View Restriction for Documents” #8
Member[4] 2008-03-16 20:19

On the next try, I got the same box that mediagirlla described.

If these mods are working, can the full code be posted so other developers have a chance to get it right?
Quote Quote
rck “Re: View Restriction for Documents” #9
Admin[1999] from Korneuburg 2008-03-16 21:06

Well... It's been a long time that I did this. Going through my original post, it reads something like

[code] $restrict_view = $this->getRestrictView();
$FormTags['RESTRICTVIEW_LABEL']=$_SESSION['translate']->it('Restrict view to group...');
$FormTags['RESTRICTVIEW'] = ""; [/code]

So exactly, that seems to be a textbox...
---
200 channels and... nothing but cats.
Quote Quote
mathijs “Re: View Restriction for Documents” #10
Member[6] 2008-04-02 12:24

I just came across this hack by accedent.

I have done the same thing a while ago, but I used the code that has been used in the pagemaster hack that restrict viewing and editing of pages to certain groups. A simple copy-paste and some small adjustments did the trick.

You can select the groups that are allowed to edit or view documents simply by selecting them from a multiple selec list.
Quote Quote

<< [ 1 ] >>
| Reply Reply |

Forum Menu

please log in for further options.

What's Related

Wiki

phpWebSite

Photo Albums

phpWebSite

Article Manager

phpWebSite

Link Manager

phpWebSite

Documents

phpWebSite

Bulletin Board

phpWebSite

FAQ

phpWebSite

Latest Updates

Free Image Hosting and Li...
created by mikepeterson, 1 week ago (, 86 views)
thread

how to start javascript !...
created by shopperpk, 3 weeks ago (, 143 views)
thread

Hello, new here !
created by shopperpk, 3 weeks ago (, 126 views)
thread

Re: how to run phpwebsite...
created by alexander, 2011-08-25 (2 rpls, 3440 views)
thread