Back to index

texmacs  1.0.7.15
tmfs_permissions.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : tmfs_permissions.cpp
00004 * DESCRIPTION: permissions for reading, writing or modifying properties
00005 * COPYRIGHT  : (C) 2007  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #include "tmfs.hpp"
00013 
00014 static string tmfs_user= "root";
00015 static hashmap<string,bool> empty_map (false);
00016 static hashmap<string,hashmap<string,bool> > tmfs_permissions (empty_map);
00017 static hashmap<string,bool> tmfs_cycle_table (false);
00018 
00019 string property_append (property p);
00020 
00021 /******************************************************************************
00022 * User management
00023 ******************************************************************************/
00024 
00025 string
00026 tmfs_create_user (string name) {
00027   if (N (tmfs_search_user (name)) != 0) return "";
00028   string user= tmfs_create_ressource ();
00029   string home= tmfs_create_file (name * " - home", "", user);
00030   properties ps;
00031   ps << seq ("user", user)
00032      << seq ("name", user, name)
00033      << seq ("owner", user, user)
00034      << seq ("in", user, user)
00035      << seq ("read", user, user)
00036      << seq ("write", user, user)
00037      << seq ("home", user, home);
00038   tmfs_save_ressource (user, "", ps);
00039   return user;
00040 }
00041 
00042 collection
00043 tmfs_search_user (string name) {
00044   properties ps; ps << seq ("name", "?user", name) << seq ("user", "?user");
00045   return as_collection (tmfs_get_solutions (ps), "?user");
00046 }
00047 
00048 void
00049 tmfs_set_user (string user) {
00050   tmfs_user= user;
00051 }
00052 
00053 string
00054 tmfs_get_user () {
00055   return tmfs_user;
00056 }
00057 
00058 /******************************************************************************
00059 * Determine permissions
00060 ******************************************************************************/
00061 
00062 bool tmfs_allows (string id, string type, string user);
00063 
00064 bool
00065 tmfs_allows_via (string id, string type, string user, string via) {
00066   (void) id;
00067   //cout << "Allows? " << id << ", " << type << ", " << user
00068   //<< " via " << via << LF;
00069   if (user == via) return true;
00070   if (is_identifier (via)) return tmfs_allows (via, type, user);
00071   return false;
00072 }
00073 
00074 bool
00075 tmfs_allows_compute (string id, string type, string user) {
00076   if (user == "root") return true;
00077   property query= seq (type, id, "?user");
00078   solutions sols= tmfs_get_solutions (query);
00079   strings a= as_strings (as_collection (sols, query));
00080   for (int i=0; i<N(a); i++)
00081     if (tmfs_allows_via (id, type, user, a[i]))
00082       return true;
00083   return false;
00084 }
00085 
00086 bool
00087 tmfs_allows (string id, string type, string user) {
00088   string s= property_append (seq (id, user));
00089   if (!tmfs_permissions[type]->contains (s)) {
00090     //cout << "Allows? " << id << ", " << type << ", " << user << INDENT << LF;
00091     if (!tmfs_permissions->contains (type))
00092       tmfs_permissions (type)= hashmap<string,bool> (false);
00093     if (tmfs_cycle_table[s]) {
00094       //cout << UNINDENT << "Aborted" << LF;
00095       return false;
00096     }
00097     tmfs_cycle_table(s)= true;
00098     bool ok= tmfs_allows_compute (id, type, user);
00099     tmfs_permissions[type](s)= ok;
00100     tmfs_cycle_table(s)= false;
00101     //cout << UNINDENT << "Allows? " << id << ", " << type << ", " << user
00102     //<< " -> " << tmfs_permissions[type][s] << LF;
00103   }
00104   return tmfs_permissions[type][s];
00105 }
00106 
00107 /******************************************************************************
00108 * Permission property and solution permissions
00109 ******************************************************************************/
00110 
00111 bool
00112 tmfs_allows (string id, string type) {
00113   return tmfs_allows (id, type, tmfs_user);
00114 }
00115 
00116 bool
00117 tmfs_allows (property p, string type) {
00118   for (int i=0; i<N(p); i++)
00119     if (is_identifier (p[i]) && !tmfs_allows (p[i], type))
00120       return false;
00121   return true;
00122 }
00123 
00124 bool
00125 tmfs_allows (solution sol, string type) {
00126   iterator<string> it= iterate (sol);
00127   while (it->busy ()) {
00128     string s= sol[it->next ()];
00129     if (is_identifier (s) && !tmfs_allows (s, type))
00130       return false;
00131   }
00132   return true;
00133 }
00134 
00135 strings
00136 tmfs_filter (strings ss, string type) {
00137   strings r;
00138   for (int i=0; i<N(ss); i++)
00139     if (tmfs_allows (ss[i], type))
00140       r << ss[i];
00141   return r;
00142 }
00143 
00144 properties
00145 tmfs_filter (properties ps, string type) {
00146   properties qs;
00147   for (int i=0; i<N(ps); i++)
00148     if (tmfs_allows (ps[i], type))
00149       qs << ps[i];
00150   return qs;
00151 }
00152 
00153 solutions
00154 tmfs_filter (solutions sols, string type) {
00155   solutions r;
00156   for (int i=0; i<N(sols); i++)
00157     if (tmfs_allows (sols[i], type))
00158       r << sols[i];
00159   return r;
00160 }
00161 
00162 /******************************************************************************
00163 * Setting attributes (i.e. properties with checked permissions)
00164 ******************************************************************************/
00165 
00166 void
00167 tmfs_set_attributes (string ressource, properties ps) {
00168   if (tmfs_allows (ressource, "owner"))
00169     tmfs_save_ressource (ressource, tmfs_load_ressource_file (ressource), ps);
00170 }
00171 
00172 properties
00173 tmfs_get_attributes (string ressource) {
00174   if (tmfs_allows (ressource, "read"))
00175     return tmfs_load_ressource_properties (ressource);
00176   return properties ();
00177 }
00178 
00179 void
00180 tmfs_add_attributes (string ressource, properties add_ps) {
00181   properties ps= tmfs_get_attributes (ressource);
00182   ps= reset (ps, add_ps);
00183   ps << add_ps;
00184   tmfs_set_attributes (ressource, ps);
00185 }
00186 
00187 void
00188 tmfs_remove_attributes (string ressource, properties sub_ps) {
00189   properties ps= tmfs_get_attributes (ressource);
00190   ps= reset (ps, sub_ps);
00191   tmfs_set_attributes (ressource, ps);
00192 }
00193 
00194 void
00195 tmfs_change_attributes (string ressource, properties mod_ps) {
00196   properties ps= tmfs_get_attributes (ressource);
00197   ps= reset (ps, widen (mod_ps));
00198   ps << mod_ps;
00199   tmfs_set_attributes (ressource, ps);
00200 }
00201 
00202 /******************************************************************************
00203 * Querying properties with permission checking
00204 ******************************************************************************/
00205 
00206 solutions
00207 tmfs_query (property query) {
00208   if (!tmfs_allows (query, "read")) return solutions ();
00209   solutions sols= tmfs_get_solutions (query);
00210   return tmfs_filter (sols, "read");
00211 }
00212 
00213 collection
00214 tmfs_query (property query, string unknown) {
00215   return as_collection (tmfs_query (query), unknown);
00216 }
00217 
00218 solutions
00219 tmfs_query (properties queries) {
00220   if (N(tmfs_filter (queries, "read")) != N(queries)) return solutions ();
00221   solutions sols= tmfs_get_solutions (queries);
00222   return tmfs_filter (sols, "read");
00223 }
00224 
00225 collection
00226 tmfs_query (properties queries, string unknown) {
00227   return as_collection (tmfs_query (queries), unknown);
00228 }