View | Details | Raw Unified | Return to bug 22343
Collapse All | Expand All

(-)a/Koha/REST/V1/Config/SMTP/Servers.pm (+194 lines)
Line 0 Link Here
1
package Koha::REST::V1::Config::SMTP::Servers;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Mojo::Base 'Mojolicious::Controller';
21
22
use Koha::SMTP::Servers;
23
24
use Try::Tiny;
25
26
=head1 API
27
28
=head2 Methods
29
30
=head3 list
31
32
Controller method that handles listing Koha::SMTP::Server objects
33
34
=cut
35
36
sub list {
37
    my $c = shift->openapi->valid_input or return;
38
39
    return try {
40
        my $smtp_servers_set = Koha::SMTP::Servers->new;
41
        my $smtp_servers = $c->objects->search( $smtp_servers_set );
42
        return $c->render(
43
            status  => 200,
44
            openapi => $smtp_servers
45
        );
46
    }
47
    catch {
48
        $c->unhandled_exception($_);
49
    };
50
}
51
52
=head3 get
53
54
Controller method that handles retrieving a single Koha::SMTP::Server object
55
56
=cut
57
58
sub get {
59
    my $c = shift->openapi->valid_input or return;
60
61
    return try {
62
        my $smtp_server = Koha::SMTP::Servers->find( $c->validation->param('smtp_server_id') );
63
64
        unless ($smtp_server) {
65
            return $c->render(
66
                status  => 404,
67
                openapi => {
68
                    error => "SMTP server not found"
69
                }
70
            );
71
        }
72
73
        my $embed = $c->stash('koha.embed');
74
75
        return $c->render(
76
            status  => 200,
77
            openapi => $smtp_server->to_api({ embed => $embed })
78
        );
79
    }
80
    catch {
81
        $c->unhandled_exception($_);
82
    }
83
}
84
85
=head3 add
86
87
Controller method that handles adding a new Koha::SMTP::Server object
88
89
=cut
90
91
sub add {
92
    my $c = shift->openapi->valid_input or return;
93
94
    return try {
95
96
        my $smtp_server = Koha::SMTP::Server->new_from_api( $c->validation->param('body') );
97
        $smtp_server->store;
98
99
        $c->res->headers->location( $c->req->url->to_string . '/' . $smtp_server->id );
100
101
        return $c->render(
102
            status  => 201,
103
            openapi => $smtp_server->to_api
104
        );
105
    }
106
    catch {
107
        if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
108
            return $c->render(
109
                status  => 409,
110
                openapi => {
111
                    error    => $_->error,
112
                    conflict => $_->duplicate_id
113
                }
114
            );
115
        }
116
117
        $c->unhandled_exception($_);
118
    };
119
}
120
121
=head3 update
122
123
Controller method that handles updating a Koha::SMTP::Server object
124
125
=cut
126
127
sub update {
128
    my $c = shift->openapi->valid_input or return;
129
130
    my $smtp_server = Koha::SMTP::Servers->find( $c->validation->param('smtp_server_id') );
131
132
    if ( not defined $smtp_server ) {
133
        return $c->render(
134
            status  => 404,
135
            openapi => {
136
                error => "Object not found"
137
            }
138
        );
139
    }
140
141
    return try {
142
        $smtp_server->set_from_api( $c->validation->param('body') );
143
        $smtp_server->store->discard_changes;
144
145
        return $c->render(
146
            status  => 200,
147
            openapi => $smtp_server->to_api
148
        );
149
    }
150
    catch {
151
        if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
152
            return $c->render(
153
                status  => 409,
154
                openapi => {
155
                    error    => $_->error,
156
                    conflict => $_->duplicate_id
157
                }
158
            );
159
        }
160
161
        $c->unhandled_exception($_);
162
    };
163
}
164
165
=head3 delete
166
167
Controller method that handles deleting a Koha::SMTP::Server object
168
169
=cut
170
171
sub delete {
172
    my $c = shift->openapi->valid_input or return;
173
174
    my $smtp_server = Koha::SMTP::Servers->find( $c->validation->param('smtp_server_id') );
175
176
    if ( not defined $smtp_server ) {
177
        return $c->render( status  => 404,
178
                           openapi => { error => "Object not found" } );
179
    }
180
181
    return try {
182
        $smtp_server->delete;
183
184
        return $c->render(
185
            status  => 204,
186
            openapi => q{}
187
        );
188
    }
189
    catch {
190
        $c->unhandled_exception($_);
191
    };
192
}
193
194
1;
(-)a/api/v1/swagger/definitions.json (+3 lines)
Lines 67-71 Link Here
67
  },
67
  },
68
  "return_claim": {
68
  "return_claim": {
69
    "$ref": "definitions/return_claim.json"
69
    "$ref": "definitions/return_claim.json"
70
  },
71
  "smtp_server": {
72
    "$ref": "definitions/smtp_server.json"
70
  }
73
  }
71
}
74
}
(-)a/api/v1/swagger/definitions/smtp_server.json (+66 lines)
Line 0 Link Here
1
{
2
  "type": "object",
3
  "properties": {
4
    "smtp_server_id": {
5
      "type": "integer",
6
      "description": "Internal SMTP server identifier"
7
    },
8
    "name": {
9
      "type": "string",
10
      "description": "Name of the SMTP server"
11
    },
12
    "library_id": {
13
      "type": [
14
        "string",
15
        "null"
16
      ],
17
      "description": "Internal identifier for the library using it. null means global"
18
    },
19
    "host": {
20
      "type": "string",
21
      "description": "SMTP host name"
22
    },
23
    "port": {
24
      "type": "integer",
25
      "description": "TCP port number"
26
    },
27
    "timeout": {
28
      "type": "integer",
29
      "description": "Maximum time in seconds to wait for server"
30
    },
31
    "ssl_mode": {
32
      "type": "string",
33
      "enum": [
34
        "disabled",
35
        "ssl",
36
        "starttls"
37
      ],
38
      "description": "If SSL/TLS will be used"
39
    },
40
    "user_name": {
41
      "type": [
42
        "string",
43
        "null"
44
      ],
45
      "description": "The user name to use for authentication (optional)"
46
    },
47
    "password": {
48
      "type": [
49
        "string",
50
        "null"
51
      ],
52
      "description": "The password to use for authentication (optional)"
53
    },
54
    "debug": {
55
      "type": "boolean",
56
      "description": "If the SMTP connection is set to debug mode"
57
    },
58
    "library": {
59
      "type": [
60
        "object",
61
        "null"
62
      ],
63
      "description": "Embedded library object (x-koha-embed)"
64
    }
65
  }
66
}
(-)a/api/v1/swagger/parameters.json (+3 lines)
Lines 29-34 Link Here
29
  "order_id_pp": {
29
  "order_id_pp": {
30
    "$ref": "parameters/order.json#/order_id_pp"
30
    "$ref": "parameters/order.json#/order_id_pp"
31
  },
31
  },
32
  "smtp_server_id_pp": {
33
    "$ref": "parameters/smtp_server.json#/smtp_server_id_pp"
34
  },
32
  "vendoridPathParam": {
35
  "vendoridPathParam": {
33
    "$ref": "parameters/vendor.json#/vendoridPathParam"
36
    "$ref": "parameters/vendor.json#/vendoridPathParam"
34
  },
37
  },
(-)a/api/v1/swagger/parameters/smtp_server.json (+9 lines)
Line 0 Link Here
1
{
2
    "smtp_server_id_pp": {
3
      "name": "smtp_server_id",
4
      "in": "path",
5
      "description": "SMTP server internal identifier",
6
      "required": true,
7
      "type": "integer"
8
    }
9
}
(-)a/api/v1/swagger/paths.json (+6 lines)
Lines 41-46 Link Here
41
  "/clubs/{club_id}/holds": {
41
  "/clubs/{club_id}/holds": {
42
    "$ref": "paths/clubs.json#/~1clubs~1{club_id}~1holds"
42
    "$ref": "paths/clubs.json#/~1clubs~1{club_id}~1holds"
43
  },
43
  },
44
  "/config/smtp_servers": {
45
    "$ref": "paths/config_smtp_servers.json#/~1config~1smtp_servers"
46
  },
47
  "/config/smtp_servers/{smtp_server_id}": {
48
    "$ref": "paths/config_smtp_servers.json#/~1config~1smtp_servers~1{smtp_server_id}"
49
  },
44
  "/holds": {
50
  "/holds": {
45
    "$ref": "paths/holds.json#/~1holds"
51
    "$ref": "paths/holds.json#/~1holds"
46
  },
52
  },
(-)a/api/v1/swagger/paths/config_smtp_servers.json (-1 / +319 lines)
Line 0 Link Here
0
- 
1
{
2
  "/config/smtp_servers": {
3
    "get": {
4
      "x-mojo-to": "Config::SMTP::Servers#list",
5
      "operationId": "listSMTPServers",
6
      "tags": [
7
        "config",
8
        "smtp"
9
      ],
10
      "produces": [
11
        "application/json"
12
      ],
13
      "parameters": [
14
        {
15
          "$ref": "../parameters.json#/match"
16
        },
17
        {
18
          "$ref": "../parameters.json#/order_by"
19
        },
20
        {
21
          "$ref": "../parameters.json#/page"
22
        },
23
        {
24
          "$ref": "../parameters.json#/per_page"
25
        },
26
        {
27
          "$ref": "../parameters.json#/q_param"
28
        },
29
        {
30
          "$ref": "../parameters.json#/q_body"
31
        },
32
        {
33
          "$ref": "../parameters.json#/q_header"
34
        }
35
      ],
36
      "responses": {
37
        "200": {
38
          "description": "A list of SMTP servers",
39
          "schema": {
40
            "type": "array",
41
            "items": {
42
              "$ref": "../definitions.json#/smtp_server"
43
            }
44
          }
45
        },
46
        "403": {
47
          "description": "Access forbidden",
48
          "schema": {
49
            "$ref": "../definitions.json#/error"
50
          }
51
        },
52
        "500": {
53
          "description": "Internal error",
54
          "schema": {
55
            "$ref": "../definitions.json#/error"
56
          }
57
        },
58
        "503": {
59
          "description": "Under maintenance",
60
          "schema": {
61
            "$ref": "../definitions.json#/error"
62
          }
63
        }
64
      },
65
      "x-koha-authorization": {
66
        "permissions": {
67
          "parameters": "1"
68
        }
69
      },
70
      "x-koha-embed": [
71
        "library"
72
      ]
73
    },
74
    "post": {
75
      "x-mojo-to": "Config::SMTP::Servers#add",
76
      "operationId": "addSMTPServer",
77
      "tags": [
78
        "config",
79
        "smtp"
80
      ],
81
      "parameters": [
82
        {
83
          "name": "body",
84
          "in": "body",
85
          "description": "A JSON object representing a new SMTP server configuration",
86
          "required": true,
87
          "schema": {
88
            "$ref": "../definitions.json#/smtp_server"
89
          }
90
        }
91
      ],
92
      "produces": [
93
        "application/json"
94
      ],
95
      "responses": {
96
        "201": {
97
          "description": "An SMTP server object",
98
          "schema": {
99
            "$ref": "../definitions.json#/smtp_server"
100
          }
101
        },
102
        "401": {
103
          "description": "Authentication required",
104
          "schema": {
105
            "$ref": "../definitions.json#/error"
106
          }
107
        },
108
        "403": {
109
          "description": "Access forbidden",
110
          "schema": {
111
            "$ref": "../definitions.json#/error"
112
          }
113
        },
114
        "409": {
115
          "description": "Conflict in creating resource",
116
          "schema": {
117
            "$ref": "../definitions.json#/error"
118
          }
119
        },
120
        "500": {
121
          "description": "Internal error",
122
          "schema": {
123
            "$ref": "../definitions.json#/error"
124
          }
125
        },
126
        "503": {
127
          "description": "Under maintenance",
128
          "schema": {
129
            "$ref": "../definitions.json#/error"
130
          }
131
        }
132
      },
133
      "x-koha-authorization": {
134
        "permissions": {
135
          "parameters": "1"
136
        }
137
      }
138
    }
139
  },
140
  "/config/smtp_servers/{smtp_server_id}": {
141
    "get": {
142
      "x-mojo-to": "Config::SMTP::Servers#get",
143
      "operationId": "getSMTPServer",
144
      "tags": [
145
        "config",
146
        "smtp"
147
      ],
148
      "parameters": [
149
        {
150
          "$ref": "../parameters.json#/smtp_server_id_pp"
151
        }
152
      ],
153
      "produces": [
154
        "application/json"
155
      ],
156
      "responses": {
157
        "200": {
158
          "description": "An SMTP server object",
159
          "schema": {
160
            "$ref": "../definitions.json#/smtp_server"
161
          }
162
        },
163
        "404": {
164
          "description": "Object not found",
165
          "schema": {
166
            "$ref": "../definitions.json#/error"
167
          }
168
        },
169
        "409": {
170
          "description": "Conflict updating resource",
171
          "schema": {
172
            "$ref": "../definitions.json#/error"
173
          }
174
        },
175
        "500": {
176
          "description": "Internal error",
177
          "schema": {
178
            "$ref": "../definitions.json#/error"
179
          }
180
        },
181
        "503": {
182
          "description": "Under maintenance",
183
          "schema": {
184
            "$ref": "../definitions.json#/error"
185
          }
186
        }
187
      },
188
      "x-koha-authorization": {
189
        "permissions": {
190
          "parameters": "1"
191
        }
192
      }
193
    },
194
    "put": {
195
      "x-mojo-to": "Config::SMTP::Servers#update",
196
      "operationId": "updateSMTPServer",
197
      "tags": [
198
        "config",
199
        "smtp"
200
      ],
201
      "parameters": [
202
        {
203
          "$ref": "../parameters.json#/smtp_server_id_pp"
204
        },
205
        {
206
          "name": "body",
207
          "in": "body",
208
          "description": "An SMTP server object",
209
          "required": true,
210
          "schema": {
211
            "$ref": "../definitions.json#/smtp_server"
212
          }
213
        }
214
      ],
215
      "produces": [
216
        "application/json"
217
      ],
218
      "responses": {
219
        "200": {
220
          "description": "An SMTP server object",
221
          "schema": {
222
            "$ref": "../definitions.json#/smtp_server"
223
          }
224
        },
225
        "401": {
226
          "description": "Authentication required",
227
          "schema": {
228
            "$ref": "../definitions.json#/error"
229
          }
230
        },
231
        "403": {
232
          "description": "Access forbidden",
233
          "schema": {
234
            "$ref": "../definitions.json#/error"
235
          }
236
        },
237
        "404": {
238
          "description": "Object not found",
239
          "schema": {
240
            "$ref": "../definitions.json#/error"
241
          }
242
        },
243
        "500": {
244
          "description": "Internal error",
245
          "schema": {
246
            "$ref": "../definitions.json#/error"
247
          }
248
        },
249
        "503": {
250
          "description": "Under maintenance",
251
          "schema": {
252
            "$ref": "../definitions.json#/error"
253
          }
254
        }
255
      },
256
      "x-koha-authorization": {
257
        "permissions": {
258
          "parameters": "1"
259
        }
260
      }
261
    },
262
    "delete": {
263
      "x-mojo-to": "Config::SMTP::Servers#delete",
264
      "operationId": "deleteSMTPServer",
265
      "tags": [
266
        "config",
267
        "smtp"
268
      ],
269
      "parameters": [
270
        {
271
          "$ref": "../parameters.json#/smtp_server_id_pp"
272
        }
273
      ],
274
      "produces": [
275
        "application/json"
276
      ],
277
      "responses": {
278
        "204": {
279
          "description": "SMTP server deleted"
280
        },
281
        "401": {
282
          "description": "Authentication required",
283
          "schema": {
284
            "$ref": "../definitions.json#/error"
285
          }
286
        },
287
        "403": {
288
          "description": "Access forbidden",
289
          "schema": {
290
            "$ref": "../definitions.json#/error"
291
          }
292
        },
293
        "404": {
294
          "description": "Object not found",
295
          "schema": {
296
            "$ref": "../definitions.json#/error"
297
          }
298
        },
299
        "500": {
300
          "description": "Internal error",
301
          "schema": {
302
            "$ref": "../definitions.json#/error"
303
          }
304
        },
305
        "503": {
306
          "description": "Under maintenance",
307
          "schema": {
308
            "$ref": "../definitions.json#/error"
309
          }
310
        }
311
      },
312
      "x-koha-authorization": {
313
        "permissions": {
314
          "parameters": "1"
315
        }
316
      }
317
    }
318
  }
319
}

Return to bug 22343