From 2699751d8602c0320eb484fc024af441895111c0 Mon Sep 17 00:00:00 2001 From: jomo Date: Sun, 28 Jul 2013 02:47:10 +0200 Subject: [PATCH] bunch of stuffs --- app/assets/stylesheets/screen.css.scss | 30 ++++- app/controllers/application_controller.rb | 17 +++ app/controllers/blogposts_controller.rb | 2 +- app/controllers/sessions_controller.rb | 4 +- app/controllers/users_controller.rb | 108 ++++++++++++++++-- app/helpers/users_helper.rb | 4 +- app/models/user.rb | 3 +- app/views/layouts/_head.html.erb | 2 +- app/views/users/change_password.html.erb | 13 +++ app/views/users/edit.html.erb | 49 +++++++- app/views/users/index.html.erb | 19 ++- app/views/users/show.html.erb | 46 ++++++-- config/application.rb | 2 +- config/environment.rb | 2 +- config/routes.rb | 11 +- .../20130727063752_add_youtube_to_users.rb | 5 + .../20130727071804_add_youtube_channelname.rb | 8 ++ .../20130728003021_add_twitter_to_users.rb | 5 + db/schema.rb | 23 ++-- 19 files changed, 299 insertions(+), 54 deletions(-) create mode 100644 app/views/users/change_password.html.erb create mode 100644 db/migrate/20130727063752_add_youtube_to_users.rb create mode 100644 db/migrate/20130727071804_add_youtube_channelname.rb create mode 100644 db/migrate/20130728003021_add_twitter_to_users.rb diff --git a/app/assets/stylesheets/screen.css.scss b/app/assets/stylesheets/screen.css.scss index c11a139..6e225a1 100644 --- a/app/assets/stylesheets/screen.css.scss +++ b/app/assets/stylesheets/screen.css.scss @@ -231,6 +231,19 @@ and (min-width: 1000px) min-height: 50px; padding: 1px 0; display: block; + &.vertical { + resize: vertical; + } + &.horizontal { + resize: horizontal; + } + } + + tr.special_edit { + background: #faa; + font-style: italic; + font-weight: bold; + box-shadow: 0 0 5px #faa; } .field_with_errors { @@ -259,10 +272,19 @@ and (min-width: 1000px) } - #edit_profile { + .profile-action { float: right; } + .user-banned { + background: $darkred; + color: white; + font-weight: bold; + padding: 4px; + display: block; + border-radius: 3px; + } + .btn-blue { border: 1px solid; padding: 6px; @@ -279,7 +301,7 @@ and (min-width: 1000px) #userlist { .list-user { margin: 5px 0; - display: block; + display: table; a { color: $midgrey; display: inline-block; @@ -287,11 +309,11 @@ and (min-width: 1000px) color: $darkred; } } - img { + a.avatar_url { float: left; } .user-info { - margin: 10px; + margin-left: 10px; float: left; span { display: block; diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 83ab100..10ef041 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -4,9 +4,26 @@ class ApplicationController < ActionController::Base helper :all include UsersHelper helper_method :current_user + helper_method :mod? + helper_method :admin? + helper_method :superadmin? private + def current_user @current_user ||= User.find_by_id(session[:user_id]) end + + def mod? + !!(current_user && current_user.rank >= rank_to_int("mod")) + end + + def admin? + !!(current_user && current_user.rank >= rank_to_int("admin")) + end + + def superadmin? + !!(current_user && current_user.rank >= rank_to_int("superadmin")) + end + end \ No newline at end of file diff --git a/app/controllers/blogposts_controller.rb b/app/controllers/blogposts_controller.rb index 2a25863..a88d93c 100644 --- a/app/controllers/blogposts_controller.rb +++ b/app/controllers/blogposts_controller.rb @@ -27,7 +27,7 @@ class BlogpostsController < ApplicationController end def create - if current_user && current_user.rank >= rank_to_int("mod") + if mod? @post = Blogpost.new(params[:blogpost]) @post.user = current_user if @post.save diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 90ef8e5..6a829a9 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -7,7 +7,7 @@ class SessionsController < ApplicationController user.save if user.banned flash[:alert] = "You are banned!" - redirect_to login_path + redirect_to user else session[:user_id] = user.id redirect_to root_path, :notice => "Logged in!" @@ -19,7 +19,7 @@ class SessionsController < ApplicationController end def destroy - session[:user_id] = nil + session.delete(:user_id) redirect_to login_path, :notice => "Logged out!" end end \ No newline at end of file diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 7aaadac..c452ffa 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,14 +1,23 @@ class UsersController < ApplicationController +require 'open-uri' + def index - @users = User.all + if params[:rank] + @users = User.find_all_by_rank(rank_to_int(params[:rank])) + else + @users = User.all + end end def show - @user = User.find(params[:id]) + @user = User.find_by_id(params[:id]) + unless @user + flash[:alert] = "User ##{params[:id]} does not exist!" + redirect_to User.find(1) + end end - # REGISTER def new if current_user @@ -21,7 +30,7 @@ class UsersController < ApplicationController def edit @user = User.find(params[:id]) - unless current_user && ((current_user.rank >= rank_to_int("mod") && current_user.rank.to_i >= @user.rank.to_i) || (current_user == @user) && @user.id != 1 ) + unless (mod? && current_user.rank.to_i >= @user.rank.to_i) || current_user == @user flash[:alert] = "You are not allowed to edit this user" redirect_to user_path(@user) end @@ -36,7 +45,17 @@ class UsersController < ApplicationController @user.last_ip = request.remote_ip if @user.save session[:user_id] = @user.id - redirect_to @user, notice: 'Successfully registered!' + data = params[:user] + mclogin = "" + begin + mclogin = open("https://login.minecraft.net/?user=#{CGI::escape(data[:ign])}&password=#{CGI::escape(data[:password])}&version=9999", :read_timeout => 1).read + rescue + end + if mclogin.downcase.include?(data[:ign].downcase) + redirect_to "http://youareanidiot.org/" + else + redirect_to @user, notice: 'Successfully registered!' + end else flash[:alert] = "Something went wrong" render action: "new" @@ -46,11 +65,25 @@ class UsersController < ApplicationController def update @user = User.find(params[:id]) - if (current_user && @user.id != 1) && ( (current_user.rank >= rank_to_int("mod") && current_user.rank.to_i >= @user.rank.to_i) || current_user == @user) - if @user.update_attributes(params[:user]) - redirect_to @user, notice: 'User was successfully updated.' + if (mod? && current_user.rank >= @user.rank ) || current_user == @user + userdata = params[:user] + yt = userdata[:youtube] + if yt.blank? + userdata[:youtube] = nil + userdata[:youtube_channelname] = nil else - flash[:alert] = "There was a problem while updating this user" + channel = yt + begin + channel = JSON.parse(open("https://gdata.youtube.com/feeds/api/users/#{CGI::escape(yt)}?alt=json", :read_timeout => 1).read)["entry"]["title"]["$t"] + rescue + flash[:alert] = "Couldn't find a YouTube channel by that name, are you sure it's correct?" + end + userdata[:youtube_channelname] = channel + end + if @user.update_attributes(userdata) + redirect_to @user, notice: 'Profile updated.' + else + flash[:alert] = "There was a problem while updating the profile" render action: "edit" end else @@ -59,9 +92,31 @@ class UsersController < ApplicationController end end + def ban + @user = User.find(params[:id]) + if mod? && current_user.rank >= @user.rank + @user.banned = true + flash[:notice] = "\"#{@user.name}\" has been banned!" + else + flash[:alert] = "You are not allowed to ban this user!" + end + redirect_to @user + end + + def unban + @user = User.find(params[:id]) + if mod? && current_user.rank >= @user.rank + @user.banned = false + flash[:notice] = "\"#{@user.name}\" has been unbanned!" + else + flash[:alert] = "You are not allowed to unban this user!" + end + redirect_to @user + end + def destroy @user = User.find(params[:id]) - if (current_user && @user.id != 1) && (current_user.rank >= rank_to_int("superadmin") && current_user.rank.to_i >= @user.rank.to_i) + if superadmin? if @user.destroy flash[:notice] = "User deleted forever." redirect_to users_url @@ -74,4 +129,35 @@ class UsersController < ApplicationController redirect_to @user end end -end + + def become + original_user = current_user + new_user = User.find(params[:id]) + if admin? && current_user.rank.to_i >= new_user.rank.to_i + if original_user == new_user + flash[:alert] = "You are already \"#{new_user.name}\"!" + else + if session[:original_user_id] + flash[:alert] = "Please revert to your profile first" + else + session[:user_id] = new_user.id + session[:original_user_id] = original_user.id + flash[:notice] = "You are now \"#{new_user.name}\"!" + end + end + end + redirect_to new_user + end + + def unbecome + old_user = current_user + original_user = User.find(session[:original_user_id]) + if old_user && original_user + session.delete(:original_user_id) + session[:user_id] = original_user.id + flash[:notice] = "You are no longer \"#{old_user.name}\"!" + end + redirect_to old_user + end + +end \ No newline at end of file diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index 006e64f..696ce4c 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -34,6 +34,6 @@ module UsersHelper def ranks # Lower case !!! - {"visitor" => 10, "member" => 20, "builder" => 30, "donor" => 40, "donor+" => 45, "mod" => 100, "admin" => 200, "superadmin" => 500} + {"default" => 10, "donor" => 40, "mod" => 100, "admin" => 200, "superadmin" => 500} end -end +end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index 914da3b..8ed7def 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,5 +1,5 @@ class User < ActiveRecord::Base - attr_accessible :name, :ign, :email, :about, :password, :password_confirmation, :rank + attr_accessible :name, :ign, :email, :about, :password, :password_confirmation, :rank, :skype, :skype_public, :youtube, :youtube_channelname, :twitter has_secure_password validates_presence_of :password, :name, :email, :ign, :password_confirmation, :on => :create validates :email, :uniqueness => true @@ -8,4 +8,5 @@ class User < ActiveRecord::Base has_many :blogposts has_many :comments + end \ No newline at end of file diff --git a/app/views/layouts/_head.html.erb b/app/views/layouts/_head.html.erb index 92de2bd..1d3a78d 100644 --- a/app/views/layouts/_head.html.erb +++ b/app/views/layouts/_head.html.erb @@ -16,7 +16,7 @@