read_documents._pike
Go to the documentation of this file.
1 /* Copyright (C) 2000-2004 Thomas Bopp, Thorsten Hampel, Ludger Merkens
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * $Id: read_documents.pike,v 1.1 2008/03/31 13:39:57 exodusd Exp $
18  */
19 inherit "/kernel/secure_mapping";
20 #include <macros.h>
21 #include <attributes.h>
22 #include <exception.h>
23 #include <events.h>
24 //! This module keeps track about what document is read by which users.
25 //! It adds a global EVENT_DOWNLOAD and stores the user downloading
26 //! in the database.
27 class read_documents : public secure_mapping{
28 public:
29 
30 
31 
32 
33 
34 mapping mReaders = ([ ]);
35 
36 
37 protected:
38  void load_module()
39 {
40  add_global_event(EVENT_DOWNLOAD, download_document, PHASE_NOTIFY);
41  LOG("table:documents - init_module()");
42  ::load_module();
43 }
44 
45 public:
46 
47 void download_document(int event, object obj, object caller)
48 {
49  object user = geteuid() || this_user();
50  array readers = get_value(obj->get_object_id());
51  if ( !arrayp(readers) ) readers = ({ });
52  if ( search(readers, user) == -1 )
53  readers += ({ user });
54  if ( mappingp(mReaders[obj]) )
55  mReaders[obj][user] = 1;
56  else {
57  mReaders[obj] = ([ ]);
58  foreach( readers, object r )
59  mReaders[obj][r] = 1;
60  }
61  set_value(obj->get_object_id(), readers);
62 }
63 
64 array get_readers(object doc)
65 {
66  array readers = get_value(doc->get_object_id());
67  if ( !arrayp(readers) ) return ({ });
68  return readers;
69 }
70 
71 bool is_reader(object doc, object user)
72 {
73  if ( mappingp(mReaders[doc]) )
74  return mReaders[doc][user];
75 
76  array readers = get_readers(doc);
77  int res = search(readers, user) >= 0;
78 
79  // just load
80  mReaders[doc] = ([ ]);
81  foreach ( readers, object u ) {
82  mReaders[doc][u] = 1;
83  }
84  return res;
85 }
86 
87 string get_identifier() { return "table:read-documents"; }
88 string get_table_name() { return "read_documents"; }
89 
90 
91 };