2 /******************************************************************************
3 * MODULE : tmfs_permissions.cpp
4 * DESCRIPTION: permissions for reading, writing or modifying properties
5 * COPYRIGHT : (C) 2007 Joris van der Hoeven
6 *******************************************************************************
7 * This software falls under the GNU general public license version 3 or later.
8 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
9 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ******************************************************************************/
14 static string tmfs_user
= "root";
15 static hashmap
<string
,bool> empty_map (false);
16 static hashmap
<string
,hashmap
<string
,bool> > tmfs_permissions (empty_map
);
17 static hashmap
<string
,bool> tmfs_cycle_table (false);
19 string
property_append (property p
);
21 /******************************************************************************
23 ******************************************************************************/
26 tmfs_create_user (string name
) {
27 if (N (tmfs_search_user (name
)) != 0) return "";
28 string user
= tmfs_create_ressource ();
29 string home
= tmfs_create_file (name
* " - home", "", user
);
31 ps
<< seq ("user", user
)
32 << seq ("name", user
, name
)
33 << seq ("owner", user
, user
)
34 << seq ("in", user
, user
)
35 << seq ("read", user
, user
)
36 << seq ("write", user
, user
)
37 << seq ("home", user
, home
);
38 tmfs_save_ressource (user
, "", ps
);
43 tmfs_search_user (string name
) {
44 properties ps
; ps
<< seq ("name", "?user", name
) << seq ("user", "?user");
45 return as_collection (tmfs_get_solutions (ps
), "?user");
49 tmfs_set_user (string user
) {
58 /******************************************************************************
59 * Determine permissions
60 ******************************************************************************/
62 bool tmfs_allows (string id
, string type
, string user
);
65 tmfs_allows_via (string id
, string type
, string user
, string via
) {
67 //cout << "Allows? " << id << ", " << type << ", " << user
68 //<< " via " << via << LF;
69 if (user
== via
) return true;
70 if (is_identifier (via
)) return tmfs_allows (via
, type
, user
);
75 tmfs_allows_compute (string id
, string type
, string user
) {
76 if (user
== "root") return true;
77 property query
= seq (type
, id
, "?user");
78 solutions sols
= tmfs_get_solutions (query
);
79 strings a
= as_strings (as_collection (sols
, query
));
80 for (int i
=0; i
<N(a
); i
++)
81 if (tmfs_allows_via (id
, type
, user
, a
[i
]))
87 tmfs_allows (string id
, string type
, string user
) {
88 string s
= property_append (seq (id
, user
));
89 if (!tmfs_permissions
[type
]->contains (s
)) {
90 //cout << "Allows? " << id << ", " << type << ", " << user << INDENT << LF;
91 if (!tmfs_permissions
->contains (type
))
92 tmfs_permissions (type
)= hashmap
<string
,bool> (false);
93 if (tmfs_cycle_table
[s
]) {
94 //cout << UNINDENT << "Aborted" << LF;
97 tmfs_cycle_table(s
)= true;
98 bool ok
= tmfs_allows_compute (id
, type
, user
);
99 tmfs_permissions
[type
](s
)= ok
;
100 tmfs_cycle_table(s
)= false;
101 //cout << UNINDENT << "Allows? " << id << ", " << type << ", " << user
102 //<< " -> " << tmfs_permissions[type][s] << LF;
104 return tmfs_permissions
[type
][s
];
107 /******************************************************************************
108 * Permission property and solution permissions
109 ******************************************************************************/
112 tmfs_allows (string id
, string type
) {
113 return tmfs_allows (id
, type
, tmfs_user
);
117 tmfs_allows (property p
, string type
) {
118 for (int i
=0; i
<N(p
); i
++)
119 if (is_identifier (p
[i
]) && !tmfs_allows (p
[i
], type
))
125 tmfs_allows (solution sol
, string type
) {
126 iterator
<string
> it
= iterate (sol
);
127 while (it
->busy ()) {
128 string s
= sol
[it
->next ()];
129 if (is_identifier (s
) && !tmfs_allows (s
, type
))
136 tmfs_filter (strings ss
, string type
) {
138 for (int i
=0; i
<N(ss
); i
++)
139 if (tmfs_allows (ss
[i
], type
))
145 tmfs_filter (properties ps
, string type
) {
147 for (int i
=0; i
<N(ps
); i
++)
148 if (tmfs_allows (ps
[i
], type
))
154 tmfs_filter (solutions sols
, string type
) {
156 for (int i
=0; i
<N(sols
); i
++)
157 if (tmfs_allows (sols
[i
], type
))
162 /******************************************************************************
163 * Setting attributes (i.e. properties with checked permissions)
164 ******************************************************************************/
167 tmfs_set_attributes (string ressource
, properties ps
) {
168 if (tmfs_allows (ressource
, "owner"))
169 tmfs_save_ressource (ressource
, tmfs_load_ressource_file (ressource
), ps
);
173 tmfs_get_attributes (string ressource
) {
174 if (tmfs_allows (ressource
, "read"))
175 return tmfs_load_ressource_properties (ressource
);
176 return properties ();
180 tmfs_add_attributes (string ressource
, properties add_ps
) {
181 properties ps
= tmfs_get_attributes (ressource
);
182 ps
= reset (ps
, add_ps
);
184 tmfs_set_attributes (ressource
, ps
);
188 tmfs_remove_attributes (string ressource
, properties sub_ps
) {
189 properties ps
= tmfs_get_attributes (ressource
);
190 ps
= reset (ps
, sub_ps
);
191 tmfs_set_attributes (ressource
, ps
);
195 tmfs_change_attributes (string ressource
, properties mod_ps
) {
196 properties ps
= tmfs_get_attributes (ressource
);
197 ps
= reset (ps
, widen (mod_ps
));
199 tmfs_set_attributes (ressource
, ps
);
202 /******************************************************************************
203 * Querying properties with permission checking
204 ******************************************************************************/
207 tmfs_query (property query
) {
208 if (!tmfs_allows (query
, "read")) return solutions ();
209 solutions sols
= tmfs_get_solutions (query
);
210 return tmfs_filter (sols
, "read");
214 tmfs_query (property query
, string unknown
) {
215 return as_collection (tmfs_query (query
), unknown
);
219 tmfs_query (properties queries
) {
220 if (N(tmfs_filter (queries
, "read")) != N(queries
)) return solutions ();
221 solutions sols
= tmfs_get_solutions (queries
);
222 return tmfs_filter (sols
, "read");
226 tmfs_query (properties queries
, string unknown
) {
227 return as_collection (tmfs_query (queries
), unknown
);