From: Andrea Zagli Date: Thu, 24 Jun 2010 19:25:48 +0000 (+0200) Subject: Added role's parents, but not managed. X-Git-Tag: 0.0.1~16 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=c6101d90f795515f895ac5b099fc8f948c6d7ab1;p=zakautho%2Flibzakautho Added role's parents, but not managed. --- diff --git a/src/autoz.c b/src/autoz.c index 90e1868..d4556e3 100644 --- a/src/autoz.c +++ b/src/autoz.c @@ -20,13 +20,29 @@ #include #endif +#include + #include "autoz.h" -typedef struct _Rule Rule; -struct _Rule +typedef struct _Role Role; +struct _Role { AutozIRole *irole; + GList *parents; + }; + +typedef struct _Resource Resource; +struct _Resource + { AutozIResource *iresource; + GList *parents; + }; + +typedef struct _Rule Rule; +struct _Rule + { + Role *role; + Resource *resource; }; static void autoz_class_init (AutozClass *class); @@ -90,6 +106,12 @@ Autoz void autoz_add_role (Autoz *autoz, AutozIRole *irole) +{ + autoz_add_role_with_parents (autoz, irole, NULL); +} + +void +autoz_add_role_with_parents (Autoz *autoz, AutozIRole *irole, ...) { AutozPrivate *priv = AUTOZ_GET_PRIVATE (autoz); @@ -99,7 +121,28 @@ autoz_add_role (Autoz *autoz, AutozIRole *irole) if (g_hash_table_lookup (priv->roles, role_id) == NULL) { - g_hash_table_insert (priv->roles, (gpointer)role_id, (gpointer)irole); + va_list args; + Role *role; + + AutozIRole *irole_parent; + Role *role_parent; + + role = (Role *)g_malloc0 (sizeof (Role)); + role->irole = irole; + role->parents = NULL; + + va_start (args, irole); + while ((irole_parent = va_arg (args, AutozIRole *)) != NULL) + { + role_parent = g_hash_table_lookup (priv->roles, autoz_irole_get_role_id (irole_parent)); + if (role_parent != NULL) + { + role->parents = g_list_append (role->parents, role_parent); + } + } + va_end (args); + + g_hash_table_insert (priv->roles, (gpointer)role_id, (gpointer)role); } } @@ -114,7 +157,13 @@ autoz_add_resource (Autoz *autoz, AutozIResource *iresource) if (g_hash_table_lookup (priv->resources, resource_id) == NULL) { - g_hash_table_insert (priv->resources, (gpointer)resource_id, (gpointer)iresource); + Resource *resource; + + resource = (Resource *)g_malloc0 (sizeof (Resource)); + resource->iresource = iresource; + resource->parents = NULL; + + g_hash_table_insert (priv->resources, (gpointer)resource_id, (gpointer)resource); } } @@ -123,25 +172,26 @@ autoz_allow (Autoz *autoz, AutozIRole *irole, AutozIResource *iresource) { AutozPrivate *priv = AUTOZ_GET_PRIVATE (autoz); - AutozIRole *real_irole; - AutozIResource *real_iresource; + Role *role; + Resource *resource; Rule r; /* check if exists */ - real_irole = g_hash_table_lookup (priv->roles, autoz_irole_get_role_id (irole)); - if (real_irole == NULL) + role = g_hash_table_lookup (priv->roles, autoz_irole_get_role_id (irole)); + if (role == NULL) { return; } - real_iresource = g_hash_table_lookup (priv->resources, autoz_iresource_get_resource_id (iresource)); - if (real_iresource == NULL) + + resource = g_hash_table_lookup (priv->resources, autoz_iresource_get_resource_id (iresource)); + if (resource == NULL) { return; } - r.irole = real_irole; - r.iresource = real_iresource; + r.role = role; + r.resource = resource; priv->rules = g_list_append (priv->rules, g_memdup (&r, sizeof (Rule))); } @@ -151,8 +201,8 @@ autoz_is_allowed (Autoz *autoz, AutozIRole *irole, AutozIResource *iresource) { gboolean ret; - AutozIRole *real_irole; - AutozIResource *real_iresource; + Role *role; + Resource *resource; GList *rules; Rule *r; @@ -161,13 +211,13 @@ autoz_is_allowed (Autoz *autoz, AutozIRole *irole, AutozIResource *iresource) ret = FALSE; - real_irole = g_hash_table_lookup (priv->roles, autoz_irole_get_role_id (irole)); - if (real_irole == NULL) + role = g_hash_table_lookup (priv->roles, autoz_irole_get_role_id (irole)); + if (role == NULL) { return ret; } - real_iresource = g_hash_table_lookup (priv->resources, autoz_iresource_get_resource_id (iresource)); - if (real_iresource == NULL) + resource = g_hash_table_lookup (priv->resources, autoz_iresource_get_resource_id (iresource)); + if (resource == NULL) { return ret; } @@ -177,9 +227,9 @@ autoz_is_allowed (Autoz *autoz, AutozIRole *irole, AutozIResource *iresource) { r = (Rule *)rules->data; - if (g_strcmp0 (autoz_irole_get_role_id (real_irole), autoz_irole_get_role_id (r->irole)) == 0) + if (g_strcmp0 (autoz_irole_get_role_id (role->irole), autoz_irole_get_role_id (r->role->irole)) == 0) { - if (g_strcmp0 (autoz_iresource_get_resource_id (real_iresource), autoz_iresource_get_resource_id (r->iresource)) == 0) + if (g_strcmp0 (autoz_iresource_get_resource_id (resource->iresource), autoz_iresource_get_resource_id (r->resource->iresource)) == 0) { ret = TRUE; break; diff --git a/src/autoz.h b/src/autoz.h index 729b501..5d645b3 100644 --- a/src/autoz.h +++ b/src/autoz.h @@ -56,6 +56,7 @@ GType autoz_get_type (void) G_GNUC_CONST; Autoz *autoz_new (void); void autoz_add_role (Autoz *autoz, AutozIRole *irole); +void autoz_add_role_with_parents (Autoz *autoz, AutozIRole *irole, ...); void autoz_add_resource (Autoz *autoz, AutozIResource *iresource); diff --git a/tests/test.c b/tests/test.c index e04d17f..9da593d 100644 --- a/tests/test.c +++ b/tests/test.c @@ -25,6 +25,7 @@ main (int argc, char **argv) { Autoz *autoz; AutozRole *role_writer; + AutozRole *role_writer_child; AutozRole *role_read_only; AutozResource *resource; @@ -35,6 +36,9 @@ main (int argc, char **argv) role_writer = autoz_role_new ("writer"); autoz_add_role (autoz, AUTOZ_IROLE (role_writer)); + role_writer_child = autoz_role_new ("writer_child"); + autoz_add_role_with_parents (autoz, AUTOZ_IROLE (role_writer_child), AUTOZ_IROLE (role_writer), NULL); + role_read_only = autoz_role_new ("read-only"); autoz_add_role (autoz, AUTOZ_IROLE (role_read_only));