Skip to content

Commit

Permalink
Add update_username to views, applications, user_activity_logs
Browse files Browse the repository at this point in the history
  • Loading branch information
binkoni committed Apr 27, 2019
1 parent c717530 commit f23561a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
14 changes: 14 additions & 0 deletions applications/user.moon
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import

import assert_valid from require "lapis.validate"
import trim_filter from require "lapis.util"
import trim from require "lapis.util"

import
ApiKeys
Expand Down Expand Up @@ -293,6 +294,19 @@ class MoonRocksUser extends lapis.Application

assert_valid @params, {
{"profile", type: "table"}
{ "username", exists: true, min_length: 2, max_length: 25 }
}

username = trim @params.username
assert_error @user\update_username(username)

import UserActivityLogs from require "models"

UserActivityLogs\create_from_request @, {
user_id: @user.id
source: "web"
action: "account.update_username"
data: {@user\get_username!, @username}
}

profile = trim_filter @params.profile,
Expand Down
3 changes: 3 additions & 0 deletions models/user_activity_logs.moon
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class UserActivityLogs extends Model

summarize: =>
switch @action
when "account.update_username"
"updated username from #{@data[1]} to #{@data[2]}"

when "account.update_profile"
parts = {}
for k, v in pairs @data
Expand Down
9 changes: 8 additions & 1 deletion models/users.moon
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,19 @@ class Users extends Model
order by similarity(username, ?) desc
]], query, query, per_page: 50

get_username: =>
@username

update_username: (name, r) =>
@update username: name
dupuser = Users\find [db.raw "lower(username)"]: name\lower!
return nil, "duplicate username" unless dupuser == nil

@update username: name, slug: name, display_name: name
if r
if r.current_user_session
r.current_user_session\revoke!
@write_session r, type: "update_username"
return true

update_password: (pass, r) =>
@update encrypted_password: bcrypt.digest pass, bcrypt.salt 5
Expand Down
11 changes: 11 additions & 0 deletions views/user_settings/profile.moon
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ class UserSettingsProfile extends require "widgets.user_settings_page"
@csrf_input!
data = @user\get_data!

div class: "row", ->
label ->
div class: "label", "Your username"

input {
type: "text"
class: "medium_input"
name: "username"
value: @user.username
}

div class: "row", ->
label ->
div class: "label", "Your website"
Expand Down

0 comments on commit f23561a

Please sign in to comment.