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

Support username change #144

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
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
1 change: 1 addition & 0 deletions models/user_sessions.moon
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class UserSessions extends Model
admin: 4
login_github: 5
register_github: 6
update_username: 7
}

@create_from_request: (req, user, more_params) =>
Expand Down
14 changes: 14 additions & 0 deletions models/users.moon
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,20 @@ class Users extends Model
order by similarity(username, ?) desc
]], query, query, per_page: 50

get_username: =>
@username

update_username: (name, r) =>
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
if r
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