Class: MyForum::UsersController

Inherits:
ApplicationController show all
Defined in:
app/controllers/my_forum/users_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate_user!, #current_user, #current_user_groups, #current_user_id, #forum_time, #is_admin?, #new_pm_count

Instance Method Details

#authorize_userObject



72
73
74
# File 'app/controllers/my_forum/users_controller.rb', line 72

def authorize_user
  redirect_to root_path unless current_user
end

#autocompleteObject



76
77
78
79
80
81
82
83
84
85
# File 'app/controllers/my_forum/users_controller.rb', line 76

def autocomplete
  #TODO mysql safe
  search_string = params[:str]
  user_list = MyForum::User.where("login LIKE '%#{search_string}%'").pluck(:login)

  respond_to do |format|
    format.html { raise 'denied' }
    format.js { render json: user_list }
  end
end

#avatar_updateObject



87
88
89
90
91
92
93
94
95
96
# File 'app/controllers/my_forum/users_controller.rb', line 87

def avatar_update
  if params[:user][:avatar]
    avatar = upload_avatar(params[:user][:avatar])
    current_user.update(avatar_url: File.join(Avatar::URL, current_user.id.to_s, avatar.file_name)) if avatar
  elsif params[:user][:avatar_url]
    current_user.update_columns(avatar_params)
  end

  redirect_to edit_user_path(current_user)
end

#createObject



27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/my_forum/users_controller.rb', line 27

def create
  @user = User.new(user_params)
  if @user.valid?
    @user.user_groups << UserGroup::MEMBER_GROUP
    @user.save
    session[:user_id] = @user.id
    redirect_to root_path
  else
    render :new
  end
end

#editObject



12
13
# File 'app/controllers/my_forum/users_controller.rb', line 12

def edit
end

#forgot_passwordObject



58
59
60
61
62
63
64
65
# File 'app/controllers/my_forum/users_controller.rb', line 58

def forgot_password
  if request.post?
    return unless user = User.find_by_email(params[:user][:email])
    UserMailer.reset_password_email(user).deliver_now
    flash[:notice] = t('.new_password_sent')
    redirect_to root_path
  end
end

#logoutObject



67
68
69
70
# File 'app/controllers/my_forum/users_controller.rb', line 67

def logout
  session[:user_id] = nil
  redirect_to admin_root_path
end

#newObject



8
9
10
# File 'app/controllers/my_forum/users_controller.rb', line 8

def new
  @user = User.new
end

#signinObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/my_forum/users_controller.rb', line 39

def 
  if request.post?
    if params[:user].blank?
      render :layout => 'signin'
      return
    end

    user = User.(params[:user][:login])
    if user && user.valid_password?(params[:user][:password])
      session[:user_id] = user.id
      redirect_to root_path
      return
    else
      flash[:error] = t('.invalid_login_or_password')
      return
    end
  end
end

#updateObject



15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/my_forum/users_controller.rb', line 15

def update
  update_password

  current_user.update!(user_update_params)

  User::ADDITIONAL_INFO_ATTRS.each do |attr|
    current_user.update(attr => params[:user][attr]) if params[:user][attr]
  end

  redirect_to edit_user_path(current_user)
end