Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invite user: basic HTML frontend #116

Merged
merged 4 commits into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/create-user/controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import Controller from '@curveball/controller';
import { Context } from '@curveball/core';
import { NotFound, UnprocessableEntity } from '@curveball/http-errors';
import * as userService from '../user/service';
import { UserTypeList } from '../user/types';
import { createUserForm } from './formats/html';

class CreateUserController extends Controller {

async get(ctx: Context) {

ctx.response.type = 'text/html';
ctx.response.body = createUserForm(ctx.query.msg, ctx.query.error);
}

async post(ctx: Context) {

const identity = ctx.request.body.identity;
const nickname = ctx.request.body.nickname;
const type = ctx.request.body.type;


if (!identity || !identity.contains(':') || identity.contains(' ')) {
throw new UnprocessableEntity('Identity must exist and must be a url');
}
if (nickname.length < 1) {
throw new UnprocessableEntity('nickname must contain at least 1 character');
}

if (!UserTypeList.includes(type)) {
throw new UnprocessableEntity('type must be one of ' + UserTypeList.join(', '));
}

try {
await userService.findByIdentity(ctx.request.body.identity);
ctx.status = 303;
ctx.response.headers.set('Location', '/create-user?error=User+already+exists');
return;
} catch (err) {
if (!(err instanceof NotFound)) {
throw err;
}
}

const newUser = await userService.save({
identity: identity,
nickname: nickname,
created: new Date(),
type: type,
active: 'active' in ctx.request.body
});
juhangsin marked this conversation as resolved.
Show resolved Hide resolved

ctx.response.status = 303;
ctx.response.headers.set('Location', '/user/' + newUser.id);

}

}

export default new CreateUserController();
13 changes: 13 additions & 0 deletions src/create-user/formats/html.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { render } from '../../templates';
import { UserTypeList } from '../../user/types';

export function createUserForm(msg: string, error: string) {

return render('create-user', {
title: 'Create User',
msg: msg,
erro: error,
type: UserTypeList,
action: '/create-user'
});
}
2 changes: 2 additions & 0 deletions src/routes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import router from '@curveball/router';
import blob from './blob/controller';
import changePassword from './changepassword/controller';
import createUser from './create-user/controller';
import group from './group/controller/collection';
import health from './health/controller';
import home from './home/controller';
Expand All @@ -23,6 +24,7 @@ import user from './user/controller/item';
const routes = [
router('/', home),
router('/user', users),
router('/create-user', createUser),
router('/user/:id', user),
router('/user/:id/log', userLog),
router('/user/:id/member', group),
Expand Down
5 changes: 3 additions & 2 deletions src/user/formats/hal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ export function collection(users: User[]) {

const hal: any = {
_links: {
self: { href: '/user' },
item: [],
'self': { href: '/user' },
'item': [],
'create-form': { href: '/create-user', title: 'Create New User'}
},
total: users.length,
};
Expand Down
46 changes: 46 additions & 0 deletions templates/create-user.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<form action="{{ action }}" method="post">

{{# if msg}}
<div class="msg info">{{ msg }}</div>
{{/if}}
{{# if error}}
<div class="msg error">{{ error }}</div>
{{/if}}

<fieldset>
<label for="nickname">
<span>Nickname</span>
<input type="text" name="nickname" id="nickname" required="required" autocomplete="nickname" />
</label>

<label for="identity">
<span>Identity (unique URL identifying this user)</span>
<input type="url" name="identity" id="identity" required="required" autocomplete="url" />
</label>

<label for="type">
<span>User Type</span>
<select name="type" id="type" required="required">
{{#each type}}
<option value="{{this}}">{{this}}</option>
{{/each}}
</select>
</label>

<label for="active">
<span>Make user active</span>
<input type="checkbox" name="active" value="1">
</label>
</fieldset>

<fieldset>
<label>
<button type="submit">Create User</button>
</label>
</fieldset>

{{#each hiddenFields}}
<input type="hidden" name="{{@key}}" value="{{this}}"/>
{{/each}}

</form>