diff --git a/app/controllers/internal/users_controller.rb b/app/controllers/internal/users_controller.rb
index db8ec59e3..716528f2d 100644
--- a/app/controllers/internal/users_controller.rb
+++ b/app/controllers/internal/users_controller.rb
@@ -2,15 +2,91 @@ class Internal::UsersController < Internal::ApplicationController
layout "internal"
def index
- @users = User.where.not(feed_url: nil)
+ @users = case params[:state]
+ when "mentors"
+ User.where(offering_mentorship: true).page(params[:page]).per(20)
+ when "mentees"
+ User.where(seeking_mentorship: true).page(params[:page]).per(20)
+ else
+ User.order("created_at DESC").page(params[:page]).per(20)
+ end
end
def edit
@user = User.find(params[:id])
end
+ def show
+ @user = User.find(params[:id])
+ @user_mentee_relationships = MentorRelationship.where(mentor_id: @user.id)
+ @user_mentor_relationships = MentorRelationship.where(mentee_id: @user.id)
+ end
+
def update
- # Only used for stripping user right now.
+ @user = User.find(params[:id])
+ @new_mentee = user_params[:add_mentee]
+ @new_mentor = user_params[:add_mentor]
+ handle_mentorship
+ add_note
+ @user.update!(user_params)
+ redirect_to "/internal/users/#{@user.id}"
+ end
+
+ def handle_mentorship
+ if user_params[:ban_from_mentorship] == "1"
+ ban_from_mentorship
+ end
+
+ if @new_mentee.blank? && @new_mentor.blank?
+ return
+ end
+ make_matches
+ end
+
+ def make_matches
+ if !@new_mentee.blank?
+ mentee = User.find(@new_mentee)
+ MentorRelationship.new(mentee_id: mentee.id, mentor_id: @user.id).save!
+ end
+
+ if !@new_mentor.blank?
+ mentor = User.find(@new_mentor)
+ MentorRelationship.new(mentee_id: @user.id, mentor_id: mentor.id).save!
+ end
+ end
+
+ def add_note
+ if user_params[:mentorship_note]
+ Note.create(
+ author_id: @current_user.id,
+ noteable_id: @user.id,
+ noteable_type: "User",
+ reason: "mentorship",
+ content: user_params[:mentorship_note],
+ )
+ end
+ end
+
+ def inactive_mentorship(mentor, mentee)
+ relationship = MentorRelationship.where(mentor_id: mentor.id, mentee_id: mentee.id)
+ relationship.update(active: false)
+ end
+
+ def ban_from_mentorship
+ @user.add_role :banned_from_mentorship
+ mentee_relationships = MentorRelationship.where(mentor_id: @user.id)
+ mentor_relationships = MentorRelationship.where(mentee_id: @user.id)
+ deactivate_mentorship(mentee_relationships)
+ deactivate_mentorship(mentor_relationships)
+ end
+
+ def deactivate_mentorship(relationships)
+ relationships.each do |relationship|
+ relationship.update(active: false)
+ end
+ end
+
+ def banish
@user = User.find(params[:id])
strip_user(@user)
redirect_to "/internal/users/#{@user.id}/edit"
@@ -62,4 +138,15 @@ class Internal::UsersController < Internal::ApplicationController
rescue StandardError => e
flash[:error] = e.message
end
+
+ private
+
+ def user_params
+ params.require(:user).permit(:seeking_mentorship,
+ :offering_mentorship,
+ :add_mentor,
+ :add_mentee,
+ :mentorship_note,
+ :ban_from_mentorship)
+ end
end
diff --git a/app/models/mentor_relationship.rb b/app/models/mentor_relationship.rb
new file mode 100644
index 000000000..b50c52b7c
--- /dev/null
+++ b/app/models/mentor_relationship.rb
@@ -0,0 +1,14 @@
+class MentorRelationship < ApplicationRecord
+ belongs_to :mentor, class_name: "User"
+ belongs_to :mentee, class_name: "User"
+ validates :mentor, presence: true
+ validates :mentee, presence: true
+ validate :check_for_same_user
+ validates_uniqueness_of :mentor_id, scope: :mentee_id
+
+ def check_for_same_user
+ if mentor_id == mentee_id
+ errors.add(:mentor_relationship, "Mentor and Mentee cannot be the same person")
+ end
+ end
+end
diff --git a/app/models/role.rb b/app/models/role.rb
index 4879394e0..f1f330626 100644
--- a/app/models/role.rb
+++ b/app/models/role.rb
@@ -27,6 +27,7 @@ class Role < ApplicationRecord
workshop_pass
video_permission
chatroom_beta_tester
+ banned_from_mentorship
comment_banned
),
}
diff --git a/app/models/user.rb b/app/models/user.rb
index 55fadd0d8..4a8af3217 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -1,7 +1,8 @@
class User < ApplicationRecord
include CloudinaryHelper
- attr_accessor :scholar_email
+ attr_accessor :scholar_email, :add_mentor, :add_mentee, :mentorship_note, :ban_from_mentorship
+
rolify
include AlgoliaSearch
include Storext.model
@@ -31,6 +32,16 @@ class User < ApplicationRecord
has_many :chat_channels, through: :chat_channel_memberships
has_many :push_notification_subscriptions, dependent: :destroy
has_many :feedback_messages
+ has_many :mentor_relationships_as_mentee,
+ class_name: "MentorRelationship", foreign_key: "mentee_id"
+ has_many :mentor_relationships_as_mentor,
+ class_name: "MentorRelationship", foreign_key: "mentor_id"
+ has_many :mentors,
+ through: :mentor_relationships_as_mentee,
+ source: :mentor
+ has_many :mentees,
+ through: :mentor_relationships_as_mentor,
+ source: :mentee
mount_uploader :profile_image, ProfileImageUploader
@@ -91,13 +102,14 @@ class User < ApplicationRecord
allow_blank: true
validates :website_url, url: { allow_blank: true, no_local: true, schemes: ["https", "http"] }
validates :website_url, :employer_name, :employer_url,
- :employment_title, :education, :location,
- length: { maximum: 100 }
- validates :mostly_work_with, :currently_learning, :currently_hacking_on,
- :available_for,
- length: { maximum: 500 }
+ length: { maximum: 100 }
+ validates :employment_title, :education, :location,
+ length: { maximum: 100 }
+ validates :mostly_work_with, :currently_learning,
+ :currently_hacking_on, :available_for,
+ length: { maximum: 500 }
validates :mentee_description, :mentor_description,
- length: { maximum: 1000 }
+ length: { maximum: 1000 }
validate :conditionally_validate_summary
validate :validate_feed_url
validate :unique_including_orgs
@@ -107,8 +119,7 @@ class User < ApplicationRecord
after_save :subscribe_to_mailchimp_newsletter
after_save :conditionally_resave_articles
after_create :estimate_default_language!
- before_update :mentor_status_update
- before_update :mentee_status_update
+ before_update :mentorship_status_update
before_validation :set_username
before_validation :downcase_email
before_validation :check_for_username_change
@@ -255,6 +266,10 @@ class User < ApplicationRecord
has_role? :warned
end
+ def banned_from_mentorship
+ has_role? :banned_from_mentorship
+ end
+
def admin?
has_role?(:super_admin)
end
@@ -515,13 +530,11 @@ class User < ApplicationRecord
follows.destroy_all
end
- def mentor_status_update
+ def mentorship_status_update
if mentor_description_changed? || offering_mentorship_changed?
self.mentor_form_updated_at = Time.now
end
- end
- def mentee_status_update
if mentee_description_changed? || seeking_mentorship_changed?
self.mentee_form_updated_at = Time.now
end
diff --git a/app/services/user_role_service.rb b/app/services/user_role_service.rb
index 80b9f1fa0..ef61148e8 100644
--- a/app/services/user_role_service.rb
+++ b/app/services/user_role_service.rb
@@ -31,6 +31,21 @@ class UserRoleService
true
end
+ def create_or_update_note(reason, content)
+ note = Note.find_by(noteable_id: @user.id, noteable_type: "User", reason: reason)
+ if note.present?
+ note.update(content: content)
+ else
+ Note.create(
+ author_id: @current_user_id,
+ noteable_id: @user.id,
+ noteable_type: "User",
+ reason: reason,
+ content: content,
+ )
+ end
+ end
+
private
def new_roles?(params)
@@ -71,21 +86,6 @@ class UserRoleService
end
end
- def create_or_update_note(reason, content)
- note = Note.find_by(noteable_id: @user.id, noteable_type: "User", reason: reason)
- if note.nil?
- Note.create(
- author_id: @current_user_id,
- noteable_id: @user.id,
- noteable_type: "User",
- reason: reason,
- content: content,
- )
- else
- note.update(content: content)
- end
- end
-
def ban(content)
@user.add_role :banned
create_or_update_note("banned", content)
diff --git a/app/views/internal/users/edit.html.erb b/app/views/internal/users/edit.html.erb
index e82b0e0b1..516ef839b 100644
--- a/app/views/internal/users/edit.html.erb
+++ b/app/views/internal/users/edit.html.erb
@@ -12,10 +12,10 @@
<% else %>
<% if @user.comments.where("created_at < ?", 7.days.ago).empty? %>
- <%= form_for([:internal, @user],confirm: "Are you absolutely sure?") do %>
+ <%= form_for(@user, :url => banish_internal_user_path(@user), :html => {:method => :post, :onsubmit => "confirm('Are you sure? This is extremely destructive and will change their username to @spam_###. Do not do it lightly.')"}) do %>
<% end %>
- This is extremely destructive. Do not do it lightly
+ This is extremely destructive and will change their username to @spam_###. Do not do it lightly.
<% else %>
You cannot take admin action from this view because this is not a newish user. For safety.
<% end %>
diff --git a/app/views/internal/users/index.html.erb b/app/views/internal/users/index.html.erb
index 70fdd87a2..1bdcccbb1 100644
--- a/app/views/internal/users/index.html.erb
+++ b/app/views/internal/users/index.html.erb
@@ -1,15 +1,20 @@
-
+<% end %>
+<%= paginate @users %>
\ No newline at end of file
diff --git a/app/views/internal/users/show.html.erb b/app/views/internal/users/show.html.erb
new file mode 100644
index 000000000..861a92d7a
--- /dev/null
+++ b/app/views/internal/users/show.html.erb
@@ -0,0 +1,100 @@
+