aboutsummaryrefslogtreecommitdiff
path: root/lib/webservices/services/user_webservice.php
blob: b96615770a861f74c0e7319f97eee2defc698199 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
# Lifter002: TODO
# Lifter007: TODO
# Lifter003: TODO
# Lifter010: TODO

/*
 * user_webservice.php - User webservice for Stud.IP
 *
 * Copyright (C) 2006 - Marcus Lunzenauer <mlunzena@uos.de>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 */


require_once 'lib/webservices/api/studip_user.php';


/**
 * Service definition regarding Stud.IP users.
 *
 * @package   studip
 * @package   webservice
 *
 * @author    mlunzena
 * @copyright (c) Authors
 */

class UserService extends AccessControlledService {


   /**
    * This parses an old-style Stud.IP message string and strips off all markup
    *
    * @param string $long_msg Stud.IP messages, concatenated with $separator
    * @param string $separator
    */
   static function parse_msg_to_clean_text($long_msg,$separator="ยง") {
       $msg = explode ($separator,$long_msg);
       $ret = [];
       for ($i=0; $i < count($msg); $i=$i+2) {
           if ($msg[$i+1]) $ret[] = trim(decodeHTML(preg_replace ("'<[\/\!]*?[^<>]*?>'si", "", $msg[$i+1])));
       }
       return join("\n", $ret);
   }

   function __construct() {
       $this->add_api_method('create_user',
           ['', 'Studip_User'],
           'Studip_User',
           'creates a new user');
       $this->add_api_method('find_user_by_user_name',
           ['', ''],
           'Studip_User',
           'finds a user by username');
       $this->add_api_method('update_user',
           ['', 'Studip_User'],
           'Studip_User',
           'updates user');
       $this->add_api_method('delete_user',
           ['', ''],
           true,
           'deletes user with given username');
       $this->add_api_method('check_credentials',
           ['', '', ''],
           true,
           'checks if given username and password match');
   }

   /**
    * This method is called before every other service method and tries to
    * authenticate an incoming request using the before_filter() in the parent.
    * Additionaly it sets up a faked Stud.IP environment using globals $auth, $user, $perm,
    * so that the service methods will run with Stud.IPs root permission.
    *
    * @param string the function's name.
    * @param array an array of arguments that will be delivered to the function.
    *
    * @return mixed if this method returns a Studip_Ws_Fault, further
    *               processing will be aborted
    */
   function before_filter(&$name, &$args) {
       global $auth, $user, $perm;

       $auth = new Seminar_Auth();
       $auth->auth = ['uid' => 'ws',
          'uname' => 'ws',
          'perm' => 'root'];
       $faked_root = new User();
       $faked_root->user_id = 'ws';
       $faked_root->username = 'ws';
       $faked_root->perms = 'root';
       $user = new Seminar_User($faked_root);
       $perm = new Seminar_Perm();
       $GLOBALS['MAIL_VALIDATE_BOX'] = false;

       return parent::before_filter($name, $args);

   }


  /**
   * Create a User using a User struct as defined in the service's WSDL.
   *
   * @param string the api key.
   * @param string a partially filled User struct.
   *
   * @return string the updated User struct. At least the user's id is filled
   *                in. If the user cannot be created, a fault containing a
   *                textual representation of the error will be delivered
   *                instead.
   */
  function create_user_action($api_key, $user) {

      $user = new Studip_User($user);

      if (!$user->save())
          return new Studip_Ws_Fault(self::parse_msg_to_clean_text($user->error));

      return $user;
  }


  /**
   * Searches for a user using the user's user name.
   *
   * @param string the api key.
   * @param string the user's username.
   *
   * @return mixed the found User struct or a fault if the user could not be
   *               found.
   */
  function find_user_by_user_name_action($api_key, $user_name) {
    $user = Studip_User::find_by_user_name($user_name);

    if (!$user)
            return new Studip_Ws_Fault('No such user.');

    return $user;
  }


  /**
   * Updates a user.
   *
   * @param string the api key.
   * @param array an array representation of an user
   *
   * @return mixed the user's User struct or a fault if the user could not be
   *               updated.
   */
  function update_user_action($api_key, $user) {

    $user = new Studip_User($user);

    if (!$user->id)
            return new Studip_Ws_Fault('You have to give the user\'s id.');

    if (!$user->save())
        return new Studip_Ws_Fault(self::parse_msg_to_clean_text($user->error));

        return Studip_User::find_by_user_id($user->id);
  }


  /**
   * Deletes a user.
   *
   * @param string the api key.
   * @param string the user's username.
   *
   * @return boolean returns TRUE if deletion was successful or a fault
   *                 otherwise.
   */
  function delete_user_action($api_key, $user_name) {

    $user = Studip_User::find_by_user_name($user_name);

    if (!$user)
            return new Studip_Ws_Fault('No such user.');

    if (!$user->destroy())
        return new Studip_Ws_Fault(self::parse_msg_to_clean_text($user->error));

    return TRUE;
  }

  /**
   * check authentication for a user.
   *
   * @param string the api key.
   * @param string the user's username.
   * @param string the user's username.
   *
   * @return boolean returns TRUE if authentication was successful or a fault
   *                 otherwise.
   */
  function check_credentials_action($api_key, $username, $password) {
      list($user_id, $error_msg, $is_new_user) = array_values(StudipAuthAbstract::CheckAuthentication($username, $password));
      if($user_id === false){
          return new Studip_Ws_Fault(strip_tags($error_msg));
      } else {
          return true;
      }
  }
}