permissions: add permissions object
authorWim Taymans <wim.taymans@collabora.co.uk>
Tue, 9 Jul 2013 12:31:15 +0000 (14:31 +0200)
committerWim Taymans <wim.taymans@collabora.co.uk>
Tue, 9 Jul 2013 12:31:15 +0000 (14:31 +0200)
Add a mini object to store permissions based on a role.

gst/rtsp-server/Makefile.am
gst/rtsp-server/rtsp-permissions.c [new file with mode: 0644]
gst/rtsp-server/rtsp-permissions.h [new file with mode: 0644]

index c414d8c..170fce5 100644 (file)
@@ -7,6 +7,7 @@ public_headers = \
                rtsp-media-factory.h \
                rtsp-media-factory-uri.h \
                rtsp-mount-points.h \
+               rtsp-permissions.h \
                rtsp-stream.h \
                rtsp-stream-transport.h \
                rtsp-session.h \
@@ -25,6 +26,7 @@ c_sources = \
        rtsp-media-factory.c \
        rtsp-media-factory-uri.c \
        rtsp-mount-points.c \
+       rtsp-permissions.c \
        rtsp-stream.c \
        rtsp-stream-transport.c \
        rtsp-session.c \
diff --git a/gst/rtsp-server/rtsp-permissions.c b/gst/rtsp-server/rtsp-permissions.c
new file mode 100644 (file)
index 0000000..4d5e192
--- /dev/null
@@ -0,0 +1,209 @@
+/* GStreamer
+ * Copyright (C) 2013 Wim Taymans <wim.taymans at gmail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include <string.h>
+
+#include "rtsp-permissions.h"
+
+typedef struct _GstRTSPPermissionsImpl
+{
+  GstRTSPPermissions permissions;
+
+  /* Roles, array of RoleEntry */
+  GArray *roles;
+} GstRTSPPermissionsImpl;
+
+typedef struct
+{
+  gchar *role;
+  GstStructure *structure;
+} RoleEntry;
+
+static void
+clear_role_entry (RoleEntry * role)
+{
+  g_free (role->role);
+  gst_structure_set_parent_refcount (role->structure, NULL);
+  gst_structure_free (role->structure);
+}
+
+//GST_DEBUG_CATEGORY_STATIC (rtsp_permissions_debug);
+//#define GST_CAT_DEFAULT rtsp_permissions_debug
+
+GST_DEFINE_MINI_OBJECT_TYPE (GstRTSPPermissions, gst_rtsp_permissions);
+
+static void gst_rtsp_permissions_init (GstRTSPPermissionsImpl * permissions,
+    GstStructure * structure);
+
+static void
+_gst_rtsp_permissions_free (GstRTSPPermissions * permissions)
+{
+  GstRTSPPermissionsImpl *impl = (GstRTSPPermissionsImpl *) permissions;
+
+  g_array_free (impl->roles, TRUE);
+
+  g_slice_free1 (sizeof (GstRTSPPermissionsImpl), permissions);
+}
+
+static GstRTSPPermissions *
+_gst_rtsp_permissions_copy (GstRTSPPermissionsImpl * permissions)
+{
+  GstRTSPPermissionsImpl *copy;
+  GstStructure *structure;
+
+  copy = g_slice_new0 (GstRTSPPermissionsImpl);
+  gst_rtsp_permissions_init (copy, structure);
+
+  return GST_RTSP_PERMISSIONS (copy);
+}
+
+static void
+gst_rtsp_permissions_init (GstRTSPPermissionsImpl * permissions,
+    GstStructure * structure)
+{
+  gst_mini_object_init (GST_MINI_OBJECT_CAST (permissions), 0,
+      GST_TYPE_RTSP_PERMISSIONS,
+      (GstMiniObjectCopyFunction) _gst_rtsp_permissions_copy, NULL,
+      (GstMiniObjectFreeFunction) _gst_rtsp_permissions_free);
+
+  permissions->roles = g_array_new (FALSE, TRUE, sizeof (RoleEntry));
+  g_array_set_clear_func (permissions->roles,
+      (GDestroyNotify) clear_role_entry);
+}
+
+/**
+ * gst_rtsp_permissions_new:
+ *
+ * Create a new empty Authorization permissions.
+ *
+ * Returns: (transfer full): a new empty authorization permissions.
+ */
+GstRTSPPermissions *
+gst_rtsp_permissions_new (void)
+{
+  GstRTSPPermissionsImpl *permissions;
+
+  permissions = g_slice_new0 (GstRTSPPermissionsImpl);
+
+  gst_rtsp_permissions_init (permissions,
+      gst_structure_new_empty ("GstRTSPPermissions"));
+
+  return GST_RTSP_PERMISSIONS (permissions);
+}
+
+/**
+ * gst_rtsp_permissions_add_role:
+ * @permissions: a #GstRTSPPermissions
+ *
+ * Add the configuration in @structure to @permissions for @role.
+ */
+void
+gst_rtsp_permissions_add_role (GstRTSPPermissions * permissions,
+    const gchar * role, GstStructure * structure)
+{
+  GstRTSPPermissionsImpl *impl = (GstRTSPPermissionsImpl *) permissions;
+  gint i, len;
+  RoleEntry item;
+  gboolean found;
+
+  g_return_if_fail (GST_IS_RTSP_PERMISSIONS (permissions));
+  g_return_if_fail (gst_mini_object_is_writable (&permissions->mini_object));
+  g_return_if_fail (role != NULL);
+  g_return_if_fail (structure != NULL);
+
+  len = impl->roles->len;
+  found = FALSE;
+  for (i = 0; i < len; i++) {
+    RoleEntry *entry = &g_array_index (impl->roles, RoleEntry, i);
+
+    if (g_str_equal (entry->role, role)) {
+      gst_structure_free (entry->structure);
+      entry->structure = structure;
+      found = TRUE;
+      break;
+    }
+  }
+  if (!found) {
+    item.role = g_strdup (role);
+    item.structure = structure;
+    gst_structure_set_parent_refcount (structure,
+        &impl->permissions.mini_object.refcount);
+    g_array_append_val (impl->roles, item);
+  }
+}
+
+/**
+ * gst_rtsp_permissions_remove_role:
+ * @permissions: a #GstRTSPPermissions
+ *
+ */
+void
+gst_rtsp_permissions_remove_role (GstRTSPPermissions * permissions,
+    const gchar * role)
+{
+  g_return_if_fail (GST_IS_RTSP_PERMISSIONS (permissions));
+  g_return_if_fail (gst_mini_object_is_writable (&permissions->mini_object));
+  g_return_if_fail (role != NULL);
+}
+
+/**
+ * gst_rtsp_permissions_get_role:
+ * @permissions: a #GstRTSPPermissions
+ *
+ */
+const GstStructure *
+gst_rtsp_permissions_get_role (GstRTSPPermissions * permissions,
+    const gchar * role)
+{
+  GstRTSPPermissionsImpl *impl = (GstRTSPPermissionsImpl *) permissions;
+  gint i, len;
+
+  g_return_val_if_fail (GST_IS_RTSP_PERMISSIONS (permissions), NULL);
+  g_return_val_if_fail (role != NULL, NULL);
+
+  len = impl->roles->len;
+  for (i = 0; i < len; i++) {
+    RoleEntry *entry = &g_array_index (impl->roles, RoleEntry, i);
+
+    if (g_str_equal (entry->role, role))
+      return entry->structure;
+  }
+  return NULL;
+}
+
+gboolean
+gst_rtsp_permissions_is_allowed (GstRTSPPermissions * permissions,
+    const gchar * role, const gchar * permission)
+{
+  const GstStructure *str;
+  gboolean result;
+
+  g_return_val_if_fail (GST_IS_RTSP_PERMISSIONS (permissions), FALSE);
+  g_return_val_if_fail (role != NULL, FALSE);
+  g_return_val_if_fail (permission != NULL, FALSE);
+
+  str = gst_rtsp_permissions_get_role (permissions, role);
+  if (str == NULL)
+    return FALSE;
+
+  if (!gst_structure_get_boolean (str, permission, &result))
+    result = FALSE;
+
+  return result;
+}
diff --git a/gst/rtsp-server/rtsp-permissions.h b/gst/rtsp-server/rtsp-permissions.h
new file mode 100644 (file)
index 0000000..5e06841
--- /dev/null
@@ -0,0 +1,95 @@
+/* GStreamer
+ * Copyright (C) 2010 Wim Taymans <wim.taymans at gmail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include <gst/gst.h>
+
+#ifndef __GST_RTSP_PERMISSIONS_H__
+#define __GST_RTSP_PERMISSIONS_H__
+
+typedef struct _GstRTSPPermissions GstRTSPPermissions;
+
+G_BEGIN_DECLS
+
+GType gst_rtsp_permissions_get_type (void);
+
+#define GST_TYPE_RTSP_PERMISSIONS        (gst_rtsp_permissions_get_type ())
+#define GST_IS_RTSP_PERMISSIONS(obj)     (GST_IS_MINI_OBJECT_TYPE (obj, GST_TYPE_RTSP_PERMISSIONS))
+#define GST_RTSP_PERMISSIONS_CAST(obj)   ((GstRTSPPermissions*)(obj))
+#define GST_RTSP_PERMISSIONS(obj)        (GST_RTSP_PERMISSIONS_CAST(obj))
+
+
+/**
+ * GstRTSPPermissions:
+ *
+ * The opaque permissions structure. It is used to define the permissions
+ * of objects in different roles.
+ */
+struct _GstRTSPPermissions {
+  GstMiniObject mini_object;
+};
+
+/* refcounting */
+/**
+ * gst_rtsp_permissions_ref:
+ * @permissions: The permissions to refcount
+ *
+ * Increase the refcount of this permissions.
+ *
+ * Returns: (transfer full): @permissions (for convenience when doing assignments)
+ */
+#ifdef _FOOL_GTK_DOC_
+G_INLINE_FUNC GstRTSPPermissions * gst_rtsp_permissions_ref (GstRTSPPermissions * permissions);
+#endif
+
+static inline GstRTSPPermissions *
+gst_rtsp_permissions_ref (GstRTSPPermissions * permissions)
+{
+  return (GstRTSPPermissions *) gst_mini_object_ref (GST_MINI_OBJECT_CAST (permissions));
+}
+
+/**
+ * gst_rtsp_permissions_unref:
+ * @permissions: (transfer full): the permissions to refcount
+ *
+ * Decrease the refcount of an permissions, freeing it if the refcount reaches 0.
+ */
+#ifdef _FOOL_GTK_DOC_
+G_INLINE_FUNC void gst_rtsp_permissions_unref (GstRTSPPermissions * permissions);
+#endif
+
+static inline void
+gst_rtsp_permissions_unref (GstRTSPPermissions * permissions)
+{
+  gst_mini_object_unref (GST_MINI_OBJECT_CAST (permissions));
+}
+
+
+GstRTSPPermissions *  gst_rtsp_permissions_new          (void);
+
+void                  gst_rtsp_permissions_add_role     (GstRTSPPermissions *permissions, const gchar *role,
+                                                         GstStructure *structure);
+void                  gst_rtsp_permissions_remove_role  (GstRTSPPermissions *permissions, const gchar *role);
+const GstStructure *  gst_rtsp_permissions_get_role     (GstRTSPPermissions *permissions, const gchar *role);
+
+gboolean              gst_rtsp_permissions_is_allowed   (GstRTSPPermissions *permissions,
+                                                         const gchar *role, const gchar *permission);
+
+G_END_DECLS
+
+#endif /* __GST_RTSP_PERMISSIONS_H__ */