94 |
94 |
95 function get_invite_by_id(event, invite_id) |
95 function get_invite_by_id(event, invite_id) |
96 local invite = invites.get_account_invite_info(invite_id); |
96 local invite = invites.get_account_invite_info(invite_id); |
97 if not invite then |
97 if not invite then |
98 return 404; |
98 return 404; |
99 end |
|
100 |
|
101 event.response.headers["Content-Type"] = json_content_type; |
|
102 return json.encode(token_info_to_invite_info(invite)); |
|
103 end |
|
104 |
|
105 function create_invite(event) |
|
106 local invite_options; |
|
107 |
|
108 local request = event.request; |
|
109 if request.body and #request.body > 0 then |
|
110 if request.headers.content_type ~= json_content_type then |
|
111 module:log("warn", "Invalid content type"); |
|
112 return 400; |
|
113 end |
|
114 invite_options = json.decode(event.request.body); |
|
115 if not invite_options then |
|
116 module:log("warn", "Invalid JSON"); |
|
117 return 400; |
|
118 end |
|
119 else |
|
120 invite_options = {}; |
|
121 end |
|
122 |
|
123 local invite; |
|
124 if invite_options.reusable then |
|
125 invite = invites.create_group(invite_options.groups, invite_options.ttl, { |
|
126 source = "admin_api/"..event.session.username; |
|
127 }); |
|
128 else |
|
129 invite = invites.create_account(nil, { |
|
130 source = "admin_api/"..event.session.username; |
|
131 groups = invite_options.groups; |
|
132 }); |
|
133 end |
|
134 if not invite then |
|
135 return 500; |
|
136 end |
99 end |
137 |
100 |
138 event.response.headers["Content-Type"] = json_content_type; |
101 event.response.headers["Content-Type"] = json_content_type; |
139 return json.encode(token_info_to_invite_info(invite)); |
102 return json.encode(token_info_to_invite_info(invite)); |
140 end |
103 end |
544 |
507 |
545 module:provides("http", { |
508 module:provides("http", { |
546 route = check_auth { |
509 route = check_auth { |
547 ["GET /invites"] = list_invites; |
510 ["GET /invites"] = list_invites; |
548 ["GET /invites/*"] = get_invite_by_id; |
511 ["GET /invites/*"] = get_invite_by_id; |
549 ["POST /invites"] = create_invite; -- Deprecated |
|
550 ["POST /invites/*"] = create_invite_type; |
512 ["POST /invites/*"] = create_invite_type; |
551 ["DELETE /invites/*"] = delete_invite; |
513 ["DELETE /invites/*"] = delete_invite; |
552 |
514 |
553 ["GET /users"] = list_users; |
515 ["GET /users"] = list_users; |
554 ["GET /users/*"] = get_user_by_name; |
516 ["GET /users/*"] = get_user_by_name; |