aboutsummaryrefslogtreecommitdiff
path: root/src/guff/models/user.cr
blob: 1862e29d6de535b0e633589a1238f6ee0c7b7d08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
class Guff::UserModel < Guff::Model
  SQL = TemplateCache.new({
    add_user: "
      INSERT INTO users(user_name) VALUES (:user_name)
    ",

    update_user: "
      UPDATE users
         SET %{sets}
       WHERE user_id = :user_id
    ",

    delete_login: "
      DELETE FROM user_logins WHERE user_id = :user_id
    ",

    add_login: "
      INSERT INTO user_logins(user_id, email, pass_hash) VALUES
        (:user_id, :email, :pass_hash)
    ",
  })

  def initialize(models : Models)
    super(models, SQL)
  end

  def get_users
    r = [] of Hash(String, String)

    all(:get_users) do |row|
      r << row
    end

    r
  end

  def add_user(name : String)
    query(:add_user, {
      "user_name": name
    }, nil)

    # return user id
    last_insert_row_id
  end

  def update_user(
    user_id : Int32,
    name    : String? = nil,
    active  : Bool?   = nil,
    role    : String? = nil,
  )
    sets = [] of String
    args = { "user_id": user_id.to_s }

    if name != nil
      args["name"] = name.not_nil!
      sets << "user_name = :name"
    end

    if active != nil
      args["is_active"] = active.not_nil! ? "1" : "0"
      sets << "is_active = :is_active"
    end

    if role != nil
      args["role_name"] = role.not_nil!
      sets << "
        role_id = (SELECT role_id
                     FROM roles
                    WHERE role_name = :role)
      "
    end

    # exec query
    query(:update_user, args, {
      "sets": sets.join(", "),
    }) if sets.size > 0
  end

  def delete_login(user_id : Int32)
    query(:delete_login, {
      "user_id": user_id.to_s
    }, nil)
  end

  def add_login(
    user_id   : Int32,
    email     : String,
    password  : String,
  )
    # TODO: check password strength
    raise "password too short" if password.length < 4

    # hash password
    pass_hash = Crypto::Bcrypt::Password.create(password, cost: 10)

    transaction do
      # clear old credentials
      delete_login(user_id)

      # add new credentials
      query(:add_login, {
        "user_id":    user_id.to_s,
        "email":      email,
        "pass_hash":  pass_hash,
      }, nil)
    end
  end
end