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 user email change #152

Open
wants to merge 4 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
13 changes: 13 additions & 0 deletions applications/user.moon
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ class MoonRocksUser extends lapis.Application

assert_valid @params, {
{"profile", type: "table"}
{"email", type: "string"}
}

profile = trim_filter @params.profile,
Expand All @@ -312,6 +313,18 @@ class MoonRocksUser extends lapis.Application
data: difference
}

if @user.email != @params.email
old_email = @user.email
@user\update_email @params.email
import UserActivityLogs from require "models"

UserActivityLogs\create_from_request @, {
user_id: @user.id
source: "web"
action: "account.update_email"
data: {old_email, @user.email}
}

redirect_to: @url_for "user_settings.profile"

}
Expand Down
7 changes: 2 additions & 5 deletions models/user_activity_logs.moon
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,10 @@ class UserActivityLogs extends Model
table.insert parts, "set #{k}"

table.concat parts, ", "
when "account.update_email"
"updated email from #{@data[1]} to #{@data[2]}"
when "account.update_password_attempt"
@data.reason
when "account.create_api_key", "account.revoke_api_key"
if @data.key
@data.key\sub(1, 10) .. "…"





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_email: 7
}

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

update_email: (email, r) =>
dupemail = Users\find [db.raw "lower(email)"]: email\lower!
return nil, "duplicate email" unless dupemail == nil

@update email: email
if r
if r.current_user_session
r.current_user_session\revoke!
@write_session r, type: "update_email"
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 email"

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

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