Overview
Comment:Implement API method to import users
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | stable
Files: files | file ages | folders
SHA3-256: d9da6117808ad2cec1308adaf6832a15a393b7cc13c178fcb0be4171578d3637
User & Date: bohwaz on 2021-10-14 01:05:12
Other Links: manifest | tags
Context
2021-10-14
01:42
Fix [289b7550cf8546ab7a9706176c16a3251eb12295] amount was not filled on initial selection, not sure why check-in: 2ade614a56 user: bohwaz tags: trunk, stable
01:05
Implement API method to import users check-in: d9da611780 user: bohwaz tags: trunk, stable
2021-10-13
16:10
Don't sanitize the file extension check-in: 51ecbdd880 user: bohwaz tags: trunk, stable
Changes

Modified src/include/lib/Garradin/API.php from [808f82b79d] to [a8567c62c9].

48
49
50
51
52
53
54







































55
56
57
58
59
60
61
			return ['results' => Recherche::rawSQL($body)];
		}
		catch (\Exception $e) {
			http_response_code(400);
			return ['error' => 'Error in SQL statement', 'sql_error' => $e->getMessage()];
		}
	}








































	protected function web(string $uri): ?array
	{
		if ($this->method != 'GET') {
			throw new APIException('Wrong request method', 400);
		}








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
			return ['results' => Recherche::rawSQL($body)];
		}
		catch (\Exception $e) {
			http_response_code(400);
			return ['error' => 'Error in SQL statement', 'sql_error' => $e->getMessage()];
		}
	}

	protected function user(string $uri): ?array
	{
		$fn = strtok($uri, '/');

		// CSV import
		if ($fn == 'import') {
			if ($this->method != 'PUT') {
				throw new APIException('Wrong request method', 400);
			}

			$admin_user_id = 1; // FIXME: should be NULL here

			$file = tempnam(CACHE_ROOT, 'tmp-import-api');

			try {
				$stdin = fopen('php://input', 'r');
				$fp = fopen($file, 'w');
				stream_copy_to_stream($stdin, $fp);
				fclose($fp);
				fclose($stdin);

				if (!filesize($file)) {
					throw new APIException('Empty CSV file', 400);
				}

				$import = new Membres\Import;
				$import->fromGarradinCSV($file, $admin_user_id);
			}
			finally {
				Utils::safe_unlink($file);
			}

			return null;
		}
		else {
			throw new APIException('Unknown user action', 404);
		}
	}

	protected function web(string $uri): ?array
	{
		if ($this->method != 'GET') {
			throw new APIException('Wrong request method', 400);
		}

118
119
120
121
122
123
124


125
126
127
128
129
130
131
		switch ($fn) {
			case 'sql':
				return $this->sql();
			case 'download':
				return $this->download();
			case 'web':
				return $this->web($uri);


			default:
				throw new APIException('Unknown path', 404);
		}
	}

	static public function dispatchURI(string $uri)
	{







>
>







157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
		switch ($fn) {
			case 'sql':
				return $this->sql();
			case 'download':
				return $this->download();
			case 'web':
				return $this->web($uri);
			case 'user':
				return $this->user($uri);
			default:
				throw new APIException('Unknown path', 404);
		}
	}

	static public function dispatchURI(string $uri)
	{