root/trunk/midgard/apis/python/py_midgard_user.c

Revision 16508, 7.5 kB (checked in by piotras, 6 months ago)

Defined NO_IMPORT_PYGOBJECT...

Line 
1 /*
2  * Copyright (C) 2007 Piotr Pokora <piotrek.pokora@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #define NO_IMPORT_PYGOBJECT
24 #include "py_midgard.h"
25
26 PyTypeObject G_GNUC_INTERNAL Pymidgard_user_Type;
27
28 #define USER_DEBUG(__name) \
29         CHECK_MGD; \
30         CLASS_METHOD_DEBUG(Pymidgard_user_Type.tp_name, __name);
31
32 static int __pyobject_to_person(PyObject *arg, gpointer *ptr)
33 {
34         if(!PyObject_IsTrue(arg))
35                 goto THROW_EXCEPTION;
36
37         PyGObject *pobj = (PyGObject *)arg;
38         if(pobj && pobj->obj != NULL) {
39                
40                 if(!G_IS_OBJECT(pobj->obj))
41                         goto THROW_EXCEPTION;
42
43                 const gchar *cname =
44                         G_OBJECT_TYPE_NAME(G_OBJECT(pobj->obj));       
45                 if(!g_str_equal(cname, "midgard_person"))
46                         goto THROW_EXCEPTION;
47                        
48         } else {
49                
50                 goto THROW_EXCEPTION;
51         }
52
53         return 1;
54
55 THROW_EXCEPTION:
56         PyErr_SetString(PyExc_TypeError, "Expected object");
57         return 0;
58 }
59
60 static int
61 __user_constructor(PyGObject *self, PyObject *args, PyObject *kwargs)
62 {
63         USER_DEBUG("__init__");
64         PyObject *pvalue = NULL;
65         PyTypeObject *pytype =
66                 py_midgard_lookup_schema_type("midgard_person");
67         if(!PyArg_ParseTuple(args,"O!:__init__", pytype, &pvalue))
68                 return -1;
69
70         MgdObject *person = MIDGARD_OBJECT(((PyGObject *)pvalue)->obj);
71         MidgardUser *user = midgard_user_new(person);
72
73         if(!user) return -1;
74
75         self->obj = G_OBJECT(user);
76
77         return 0;
78 }
79
80 static PyObject *
81 pymidgard_user_auth(PyGObject *self, PyObject *args)
82 {
83         USER_DEBUG("auth");
84         const gchar *login, *pass, *sitegroup;
85         gboolean trusted = FALSE;
86         if(!PyArg_ParseTuple(args, "ss|zb", &login, &pass, &sitegroup, &trusted))
87                 return NULL;
88
89         MidgardConnection *mgd =
90                 _py_midgard_connection_singleton_get();
91
92         MidgardUser *user =
93                 midgard_user_auth(mgd, login, pass, sitegroup, trusted);
94
95         if(user == NULL)
96                 Py_RETURN_NONE;
97        
98         GValue gval = {0, };
99         g_value_init(&gval, G_TYPE_OBJECT);
100         g_value_set_object(&gval, G_OBJECT(user));
101         PyObject *pval = pyg_value_as_pyobject((const GValue *)&gval, FALSE);
102        
103         return pval;
104 }
105
106 static PyObject *
107 pymidgard_user_is_user(PyGObject *self, PyObject *args)
108 {
109         USER_DEBUG("is_user");
110         if(!PyArg_ParseTuple(args, ""))
111                 return NULL;
112
113         MidgardUser *user = MIDGARD_USER(self->obj);
114
115         g_assert(user != NULL);
116
117         if(midgard_user_is_user(user))
118                 Py_RETURN_TRUE;
119
120         Py_RETURN_FALSE;
121 }
122
123 static PyObject *
124 pymidgard_user_is_admin(PyGObject *self, PyObject *args)
125 {
126         USER_DEBUG("is_admin");
127         if(!PyArg_ParseTuple(args, ""))
128                 return NULL;
129
130         MidgardUser *user = MIDGARD_USER(self->obj);
131
132         g_assert(user != NULL);
133
134         if(midgard_user_is_admin(user))
135                 Py_RETURN_TRUE;
136
137         Py_RETURN_FALSE;
138 }
139
140 static PyObject *
141 pymidgard_user_is_root(PyGObject *self, PyObject *args)
142 {
143         USER_DEBUG("is_root");
144         if(!PyArg_ParseTuple(args, ""))
145                 return NULL;
146
147         MidgardUser *user = MIDGARD_USER(self->obj);
148
149         g_assert(user != NULL);
150
151         if(midgard_user_is_root(user))
152                 Py_RETURN_TRUE;
153
154         Py_RETURN_FALSE;
155 }
156
157 static PyObject *
158 pymidgard_user_set_active(PyGObject *self, PyObject *args)
159 {
160         USER_DEBUG("set_active");
161         gboolean flag = FALSE;
162         if(!PyArg_ParseTuple(args, "b", &flag))
163                 return NULL;
164
165         MidgardUser *user = MIDGARD_USER(self->obj);
166
167         g_assert(user != NULL);
168
169         if(midgard_user_set_active(user, flag))
170                 Py_RETURN_TRUE;
171
172         Py_RETURN_FALSE;
173 }
174
175 static PyObject *
176 pymidgard_user_password(PyGObject *self, PyObject *args)
177 {
178         USER_DEBUG("password");
179         const gchar *name, *password;
180         guint hashtype = 0;
181         gboolean flag = FALSE;
182         if(!PyArg_ParseTuple(args, "ss|H", &name, &password, &hashtype))
183                 return NULL;
184
185         MidgardUser *user = MIDGARD_USER(self->obj);
186
187         g_assert(user != NULL);
188
189         if(midgard_user_password(user, name, password, hashtype))
190                 Py_RETURN_TRUE;
191
192         Py_RETURN_FALSE;
193 }
194
195 static PyObject *
196 pymidgard_user_get_person(PyGObject *self, PyObject *args)
197 {
198         USER_DEBUG("get_person");
199        
200         if(!PyArg_ParseTuple(args, ""))
201                 return NULL;
202        
203         MgdObject *person =
204                 midgard_user_get_person(MIDGARD_USER(self->obj));
205        
206         if(person == NULL)
207                 Py_RETURN_NONE;
208
209         GValue gval = {0, };
210         g_value_init(&gval, G_TYPE_OBJECT);
211         g_value_set_object(&gval, G_OBJECT(person));
212         PyObject *pval = pyg_value_as_pyobject((const GValue *)&gval, FALSE);
213        
214         return pval;
215 }
216
217 static PyMethodDef pymidgard_user_methods[] = {
218         { "auth", (PyCFunction)pymidgard_user_auth, METH_VARARGS | METH_STATIC },
219         { "is_user", (PyCFunction)pymidgard_user_is_user, METH_VARARGS },
220         { "is_admin", (PyCFunction)pymidgard_user_is_admin, METH_VARARGS },
221         { "is_root", (PyCFunction)pymidgard_user_is_root, METH_VARARGS },
222         { "set_active", (PyCFunction)pymidgard_user_set_active, METH_VARARGS },
223         { "password", (PyCFunction)pymidgard_user_password, METH_VARARGS },
224         { "get_person", (PyCFunction)pymidgard_user_get_person, METH_VARARGS },
225         { NULL, NULL, 0 }
226 };
227
228 PyTypeObject G_GNUC_INTERNAL Pymidgard_user_Type = {
229     PyObject_HEAD_INIT(NULL)
230     0,                                 /* ob_size */
231     "user",                   /* tp_name */
232     sizeof(PyGObject),          /* tp_basicsize */
233     0,                                 /* tp_itemsize */
234     /* methods */
235     (destructor)0,        /* tp_dealloc */
236     (printfunc)0,                      /* tp_print */
237     (getattrfunc)0,       /* tp_getattr */
238     (setattrfunc)0,       /* tp_setattr */
239     (cmpfunc)0,           /* tp_compare */
240     (reprfunc)0,             /* tp_repr */
241     (PyNumberMethods*)0,     /* tp_as_number */
242     (PySequenceMethods*)0, /* tp_as_sequence */
243     (PyMappingMethods*)0,   /* tp_as_mapping */
244     (hashfunc)0,             /* tp_hash */
245     (ternaryfunc)0,          /* tp_call */
246     (reprfunc)0,              /* tp_str */
247     (getattrofunc)0,     /* tp_getattro */
248     (setattrofunc)0,     /* tp_setattro */
249     (PyBufferProcs*)0,  /* tp_as_buffer */
250     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,                      /* tp_flags */
251     NULL,                        /* Documentation string */
252     (traverseproc)0,     /* tp_traverse */
253     (inquiry)0,             /* tp_clear */
254     (richcmpfunc)0,   /* tp_richcompare */
255     offsetof(PyGObject, weakreflist),             /* tp_weaklistoffset */
256     (getiterfunc)0,          /* tp_iter */
257     (iternextfunc)0,     /* tp_iternext */
258     pymidgard_user_methods, /* tp_methods */
259     (struct PyMemberDef*)0,              /* tp_members */
260     (struct PyGetSetDef*)0,  /* tp_getset */
261     NULL,                              /* tp_base */
262     NULL,                              /* tp_dict */
263     (descrgetfunc)0,    /* tp_descr_get */
264     (descrsetfunc)0,    /* tp_descr_set */
265     offsetof(PyGObject, inst_dict),                 /* tp_dictoffset */
266     (initproc)__user_constructor,              /* tp_init */
267     (allocfunc)0,           /* tp_alloc */
268     (newfunc)0,               /* tp_new */
269     (freefunc)0,             /* tp_free */
270     (inquiry)0              /* tp_is_gc */
271 };
272
273 void py_midgard_user_register_class(
274                 PyObject *d, gpointer pygobject_type)
275 {
276         pygobject_register_class(d,
277                         "user",
278                         MIDGARD_TYPE_USER,
279                         &Pymidgard_user_Type,
280                         Py_BuildValue("(O)", pygobject_type));
281 }
Note: See TracBrowser for help on using the browser.