Skip to content

Commit

Permalink
Merge pull request #1441 from codidact/art/1439/me-links
Browse files Browse the repository at this point in the history
Add support for 'me' as a user ID
  • Loading branch information
Oaphi authored Oct 26, 2024
2 parents 78cbfb2 + 1faf2d9 commit 73f5473
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/controllers/flags_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def new
end

def history
@user = User.find(params[:id])
@user = helpers.user_with_me params[:id]
unless @user == current_user || (current_user.is_admin || current_user.is_moderator)
not_found
return
Expand Down
11 changes: 10 additions & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ def posts
end
end

def my_network
redirect_to network_path(current_user)
end

def network
@communities = Community.all
render layout: 'without_sidebar'
Expand Down Expand Up @@ -632,7 +636,12 @@ def filter_params
end

def set_user
@user = user_scope.find_by(id: params[:id])
user_id = if params[:id] == 'me' && user_signed_in?
current_user.id
else
params[:id]
end
@user = user_scope.find_by(id: user_id)
not_found if @user.nil?
end

Expand Down
13 changes: 13 additions & 0 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,17 @@ def sso_sign_in_enabled?
def devise_sign_in_enabled?
SiteSetting['MixedSignIn'] || !sso_sign_in_enabled?
end

##
# Returns a user corresponding to the ID provided, with the caveat that if +user_id+ is 'me' and there is a user
# signed in, the signed in user will be returned. Use for /users/me links.
# @param [String] user_id The user ID to find, from +params+
# @return [User] The User object
def user_with_me(user_id)
if user_id == 'me' && user_signed_in?
current_user
else
User.find(user_id)
end
end
end

0 comments on commit 73f5473

Please sign in to comment.