From 6b9ca27c67d043a8737da652bf7915645628db4c Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Fri, 1 Apr 2016 15:58:08 -0400 Subject: add_user test --- src/guff/api/methods.cr | 37 +++++++++++++++++ src/guff/api/test.cr | 9 ++++- src/guff/models.cr | 1 + src/guff/models/user.cr | 56 ++++++++++++++++++++------ src/guff/views/ecrs/test/auth.ecr | 84 +++++++++++++++++++++++++++++++++++++++ src/guff/views/html/test/auth.cr | 24 +++++++++++ 6 files changed, 197 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/guff/api/methods.cr b/src/guff/api/methods.cr index 8df48d5..970e41f 100644 --- a/src/guff/api/methods.cr +++ b/src/guff/api/methods.cr @@ -366,6 +366,42 @@ module Guff }, }, }, + + "add_user": { + text: "Add new user", + + args: { + "name": { + text: "Name of user", + type: :text, + required: true, + }, + + "active": { + text: "Is this user active?", + type: :bool, + required: false, + }, + + "role": { + text: "Role of user", + type: :text, + required: false, + }, + + "email": { + text: "Email address of user", + type: :text, + required: false, + }, + + "password": { + text: "Password of user", + type: :text, + required: false, + }, + }, + }, } } @@ -375,6 +411,7 @@ module Guff int: /^\d+$/, int_list: /^\d+(?:,\d+)*$/, state: /^(?:draft|posted|deleted)$/, + bool: /^t|f$/, # FIXME: lock these down more json: /.*/, diff --git a/src/guff/api/test.cr b/src/guff/api/test.cr index 3a53bd1..059f670 100644 --- a/src/guff/api/test.cr +++ b/src/guff/api/test.cr @@ -79,7 +79,12 @@ module Guff::API::TestAPI context : HTTP::Server::Context, args : Hash(String, String) ) - user_id = @models.user.add_user(args["name"]) + user_id = @models.user.add_user(args["name"].not_nil!, + active: args["active"]? ? args["active"] == "t" : nil, + role: args["role"]?, + email: args["name"]?, + password: args["password"]?, + ) { "ok": true, "user_id": user_id } end @@ -87,7 +92,7 @@ module Guff::API::TestAPI context : HTTP::Server::Context, args : Hash(String, String) ) - @models.user.update_user(args["user_id"].to_i, + @models.user.update_user(args["user_id"].to_i64, name: args["name"]? ? args["name"] : nil, active: args["active"]? ? args["active"] == "t" : nil, role: args["role"]? ? args["role"] : nil diff --git a/src/guff/models.cr b/src/guff/models.cr index 4da8c2d..4b6a46f 100644 --- a/src/guff/models.cr +++ b/src/guff/models.cr @@ -35,6 +35,7 @@ module Guff site: SiteModel, session: SessionModel, user: UserModel, + role: RoleModel, }) end end diff --git a/src/guff/models/user.cr b/src/guff/models/user.cr index 1862e29..98ae10b 100644 --- a/src/guff/models/user.cr +++ b/src/guff/models/user.cr @@ -1,3 +1,5 @@ +require "crypto/bcrypt" + class Guff::UserModel < Guff::Model SQL = TemplateCache.new({ add_user: " @@ -34,17 +36,47 @@ class Guff::UserModel < Guff::Model r end - def add_user(name : String) - query(:add_user, { - "user_name": name - }, nil) + def add_user( + name : String, + active : Bool? = nil, + role : String? = nil, + email : String? = nil, + password : String? = nil, + ) + user_id = -1_i64 + + transaction do + # add user + query(:add_user, { + "user_name": name + }, nil) + + # get user_id + user_id = last_insert_row_id as Int64 + + # set active and/or role + if active || role + update_user(user_id, + active: active, + role: role, + ) + end + + # add login + if email || password + add_login(user_id, + email.not_nil!, + password.not_nil! + ) + end + end - # return user id - last_insert_row_id + # return id + user_id end def update_user( - user_id : Int32, + user_id : Int64, name : String? = nil, active : Bool? = nil, role : String? = nil, @@ -63,7 +95,7 @@ class Guff::UserModel < Guff::Model end if role != nil - args["role_name"] = role.not_nil! + args["role"] = role.not_nil! sets << " role_id = (SELECT role_id FROM roles @@ -77,22 +109,22 @@ class Guff::UserModel < Guff::Model }) if sets.size > 0 end - def delete_login(user_id : Int32) + def delete_login(user_id : Int64) query(:delete_login, { "user_id": user_id.to_s }, nil) end def add_login( - user_id : Int32, + user_id : Int64, email : String, password : String, ) # TODO: check password strength - raise "password too short" if password.length < 4 + raise "password too short" if password.size < 4 # hash password - pass_hash = Crypto::Bcrypt::Password.create(password, cost: 10) + pass_hash = Crypto::Bcrypt::Password.create(password, cost: 10).to_s transaction do # clear old credentials diff --git a/src/guff/views/ecrs/test/auth.ecr b/src/guff/views/ecrs/test/auth.ecr index c28f178..89d3283 100644 --- a/src/guff/views/ecrs/test/auth.ecr +++ b/src/guff/views/ecrs/test/auth.ecr @@ -46,5 +46,89 @@ + +
+
+ + + Create User + +
+ +
+
+ + + +
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+ +
+ +
+
+
diff --git a/src/guff/views/html/test/auth.cr b/src/guff/views/html/test/auth.cr index 9f5d1ad..1baa7de 100644 --- a/src/guff/views/html/test/auth.cr +++ b/src/guff/views/html/test/auth.cr @@ -5,6 +5,18 @@ class Guff::TestAuthHTMLView TITLE = "Guff Auth Test" FEATURES = %w{bootstrap font-awesome guff/util} + TEMPLATES = TemplateCache.new({ + role: " + + " + }) + def self.run(models, context : HTTP::Server::Context) new(models).run(context) end @@ -24,5 +36,17 @@ class Guff::TestAuthHTMLView HTML.escape(s || "") end + def get_roles + @models.role.get_roles.map { |row| + name = row["role_name"] as String + + TEMPLATES[:role].run({ + "name": h(name), + "text": h(row["role_desc"] as String), + "selected": (name == "guest") ? "selected='selected'" : "" + }) + }.join + end + ECR.def_to_s("./src/guff/views/ecrs/test/auth.ecr") end -- cgit v1.2.3