summaryrefslogtreecommitdiffstats
path: root/lib/api/kolab_user_actions.php
blob: b0b4e534ef76eb8285c446e23b252883adaa6f41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php

    /**
     *
     */
    class kolab_user_actions extends kolab_api_service
    {
        public function capabilities($domain)
        {
            return array(
                    'add' => 'w',
                    'delete' => 'w',
//                     'edit' => 'w',
//                     'find' => 'r',
//                     'find_by_any_attribute' => 'r',
//                     'find_by_attribute' => 'r',
//                     'find_by_attributes' => 'r',
                    'info' => 'r',
                );
        }

        public function user_add($getdata, $postdata) {
            if (!isset($postdata['user_type_id'])) {
                throw new Exception("No user type ID specified", 346781);
            }

            $user_type = mysql_fetch_assoc(query("SELECT attributes FROM user_types WHERE id = '" . $postdata['user_type_id'] ."'"));

            $uta = json_decode(unserialize($user_type['attributes']), true);

            $user_attributes = Array();

            if (isset($uta['form_fields'])) {
                foreach ($uta['form_fields'] as $key => $value) {
                    error_log("form field $key");
                    if (!isset($postdata[$key]) || empty($postdata[$key])) {
                        throw new Exception("Missing input value for $key", 345);
                    } else {
                        $user_attributes[$key] = $postdata[$key];
                    }
                }
            }

            if (isset($uta['auto_form_fields'])) {
                foreach ($uta['auto_form_fields'] as $key => $value) {
                    if (!isset($postdata[$key])) {
                        throw new Exception("Key not set: " . $key, 12356);
                    } else {
                        $user_attributes[$key] = $postdata[$key];
                    }
                }
            }

            if (isset($uta['fields'])) {
                foreach ($uta['fields'] as $key => $value) {
                    if (!isset($postdata[$key]) || empty($postdata[$key])) {
                        $user_attributes[$key] = $uta['fields'][$key];
                    } else {
                        $user_attributes[$key] = $postdata[$key];
                    }
                }
            }

            $auth = Auth::get_instance();
            $result = $auth->user_add($user_attributes, $postdata['user_type_id']);
            if ($result) {
                return $user_attributes;
            } else {
                return FALSE;
            }
        }

        public function user_delete($getdata, $postdata) {
            // TODO: Input validation
            $auth = Auth::get_instance();
            if (!isset($postdata['user'])) {
                return FALSE;
            }

            $result = $auth->user_delete($postdata['user']);
            if ($result) {
                return $result;
            } else {
                return FALSE;
            }
        }

        public function user_info($getdata, $postdata) {
            $auth = Auth::get_instance();

            if (!isset($getdata['user'])) {
                return FALSE;
            }

            $result = $auth->user_info($getdata['user']);

            if ($result) {
                return $result;
            } else {
                return FALSE;
            }
        }
    }

?>