| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 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, |
|---|
| 231 |
"user", |
|---|
| 232 |
sizeof(PyGObject), |
|---|
| 233 |
0, |
|---|
| 234 |
|
|---|
| 235 |
(destructor)0, |
|---|
| 236 |
(printfunc)0, |
|---|
| 237 |
(getattrfunc)0, |
|---|
| 238 |
(setattrfunc)0, |
|---|
| 239 |
(cmpfunc)0, |
|---|
| 240 |
(reprfunc)0, |
|---|
| 241 |
(PyNumberMethods*)0, |
|---|
| 242 |
(PySequenceMethods*)0, |
|---|
| 243 |
(PyMappingMethods*)0, |
|---|
| 244 |
(hashfunc)0, |
|---|
| 245 |
(ternaryfunc)0, |
|---|
| 246 |
(reprfunc)0, |
|---|
| 247 |
(getattrofunc)0, |
|---|
| 248 |
(setattrofunc)0, |
|---|
| 249 |
(PyBufferProcs*)0, |
|---|
| 250 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
|---|
| 251 |
NULL, |
|---|
| 252 |
(traverseproc)0, |
|---|
| 253 |
(inquiry)0, |
|---|
| 254 |
(richcmpfunc)0, |
|---|
| 255 |
offsetof(PyGObject, weakreflist), |
|---|
| 256 |
(getiterfunc)0, |
|---|
| 257 |
(iternextfunc)0, |
|---|
| 258 |
pymidgard_user_methods, |
|---|
| 259 |
(struct PyMemberDef*)0, |
|---|
| 260 |
(struct PyGetSetDef*)0, |
|---|
| 261 |
NULL, |
|---|
| 262 |
NULL, |
|---|
| 263 |
(descrgetfunc)0, |
|---|
| 264 |
(descrsetfunc)0, |
|---|
| 265 |
offsetof(PyGObject, inst_dict), |
|---|
| 266 |
(initproc)__user_constructor, |
|---|
| 267 |
(allocfunc)0, |
|---|
| 268 |
(newfunc)0, |
|---|
| 269 |
(freefunc)0, |
|---|
| 270 |
(inquiry)0 |
|---|
| 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 |
} |
|---|