view src/nabble/view/web/user/UserEditorNamespace.java @ 0:7ecd1a4ef557

add content
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 21 Mar 2019 19:15:52 -0600
parents
children
line wrap: on
line source

package nabble.view.web.user;

import nabble.model.Message;
import nabble.model.ModelException;
import nabble.model.User;
import nabble.naml.compiler.Command;
import nabble.naml.compiler.CommandSpec;
import nabble.naml.compiler.IPrintWriter;
import nabble.naml.compiler.Interpreter;
import nabble.naml.compiler.Namespace;
import nabble.naml.namespaces.TemplateException;
import nabble.view.lib.Permissions;
import nabble.view.web.template.ServletNamespace;


@Namespace (
	name = "user_editor",
	global = true
)
public final class UserEditorNamespace {
	private final User user;

	public UserEditorNamespace(User user) {
		this.user = user;
	}

	public static final CommandSpec save_user = CommandSpec.NO_OUTPUT;

	@Command public void save_user(IPrintWriter out,Interpreter interp) {
		user.update();
	}

	public static final CommandSpec add_to_group = new CommandSpec.Builder()
		.dotParameter("group")
		.build()
	;

	@Command public void add_to_group(IPrintWriter out,Interpreter interp)
		throws TemplateException
	{
		String group = interp.getArgString("group");
		Permissions.addToGroup(user, group);
	}

	public static final CommandSpec remove_from_group = new CommandSpec.Builder()
		.dotParameter("group")
		.build()
	;

	@Command public void remove_from_group(IPrintWriter out,Interpreter interp)
		throws TemplateException
	{
		String group = interp.getArgString("group");
		Permissions.removeFromGroup(user, group);
	}

	public static final CommandSpec set_signature = CommandSpec.NO_OUTPUT()
		.parameters("signature","is_html")
		.requiredInStack(ServletNamespace.class)
		.build()
	;

	@Command public void set_signature(IPrintWriter out,Interpreter interp)
		throws ModelException
	{
		String signature = interp.getArgString("signature");
		boolean isHtml = interp.getArgAsBoolean("is_html", false);
		Message.Format fmt = isHtml? Message.Format.HTML : Message.Format.TEXT;
		ServletNamespace servletNs = interp.getFromStack(ServletNamespace.class);
		user.setSignature(signature, fmt);
	}

	public static final CommandSpec set_name = CommandSpec.NO_OUTPUT()
		.parameters("name")
		.build()
	;

	@Command public void set_name(IPrintWriter out,Interpreter interp)
		throws ModelException
	{
		String name = interp.getArgString("name");
		user.setName(name);
	}

	public static final CommandSpec set_password = CommandSpec.NO_OUTPUT()
		.parameters("password1", "password2")
		.build()
	;

	@Command public void set_password(IPrintWriter out,Interpreter interp)
		throws TemplateException
	{
		String password1 = interp.getArgString("password1");
		String password2 = interp.getArgString("password2");
		if (password1.trim().isEmpty() && password2.trim().isEmpty())
			return; // skip change
		if (password1.equals(password2)) {
			user.setPassword(password1);
		} else
			throw ModelException.newInstance("passwords_dont_match");
	}

}