Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7545839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:02:13+00:00 2026-05-30T09:02:13+00:00

i know it’s a silly one but i want to know how can we

  • 0

i know it’s a silly one but i want to know how can we create a current_user method to get access throughout the app without using any gem or plugin ? To test it i created an app that make a user able to share files and folders.How to create such method that a user can only access his folder and files?Here is my code sample:

Login controller:

class LoginController < ApplicationController
 layout 'signup'
  #to skip checking the authentication and authorization.
  skip_before_filter  :check_authentication, :check_authorization

  def index
  end

  def authenticate
        if request.post?
            user = User.authenticate(params[:username],params[:password])
            if user
        session[:current_user_id]=user.id
        session[:name]= user.first_name
                puts "session name #{session[:name]}"
                redirect_to(:subdomain => user.company.subdomain, :controller => :dashboard)
      else
        flash.now[:notice] = "Invalid user/password combination"
      end  

        end
  end

  def destroy
    session[:current_user_id] = nil
    reset_session
    flash[:notice] = "You have been successfully logged out."
    redirect_to root_url
  end

end 

User model:

require 'digest/sha1'



 class User < ActiveRecord::Base

    #sharering method start
      after_create :check_and_assign_shared_ids_to_shared_folders  
      #this is to make sure the new user ,of which the email addresses already used to share folders by others, to have access to those folders  
      def check_and_assign_shared_ids_to_shared_folders      
        #First checking if the new user's email exists in any of ShareFolder records  
        shared_folders_with_same_email = SharedFolder.find_all_by_shared_email(self.email)  

        if shared_folders_with_same_email        
         #loop and update the shared user id with this new user id   
          shared_folders_with_same_email.each do |shared_folder|  
             shared_folder.shared_user_id = self.id  
             shared_folder.save  
          end  
        end      
      end 

        #to check if a user has acess to this specific folder  
      def has_share_access?(folder)  
          #has share access if the folder is one of one of his own  
          return true if self.folders.include?(folder)  

          #has share access if the folder is one of the shared_folders_by_others  
          return true if self.shared_folders_by_others.include?(folder)  

          #for checking sub folders under one of the being_shared_folders  
          return_value = false  

         folder.ancestors.each do |ancestor_folder|  

            return_value = self.being_shared_folders.include?(ancestor_folder)  
           if return_value #if it's true  
              return true  
            end  
          end  

          return false  
      end  
    #sharing method end

      def self.authenticate(name, password)
        user = self.find_by_username(name)

        if user
          expected_password = encrypt_password(password, user.salt)
          if user.hashed_password != expected_password
            user = nil
          end
        end
        user
      end

      #'password' is a virtual attribute
      def password
        @password
      end

      def password= (pwd)
        @password =pwd
        return if pwd.blank?
        create_new_salt
        self.hashed_password = User.encrypt_password( self.password, self.salt)
      end

        def self.users_in_company(user_id)
            User.find(user_id).company.users 
        end  

     private
      def password_non_blank
        errors.add(:password, "Missing password, please enter your password") if hashed_password.blank?
      end

      def create_new_salt
        self.salt = self.object_id.to_s + rand.to_s
      end

      def self.encrypt_password(password, salt)
        string_to_hash = password +"prftnxt" + salt
        Digest::SHA1.hexdigest(string_to_hash)
      end

    end

i want to access all files as “current_user.files” is it possible without any gem?

Application helper:

module ApplicationHelper
#for current user to use through out the app
   def current_user
     @current_user ||= session[:current_user_id] && User.find_by_id(session[:current_user_id]) # Use find_by_id to get nil instead of an error if user doesn't exist
   end

end

Application controller:

class ApplicationController < ActionController::Base
      include UrlHelper 
      #include ApplicationHelper
      helper_method :current_user #make this method available in views
      protect_from_forgery


#  def current_user
#    @current_user ||= session[:current_user_id] && User.find_by_id(session[:current_user_id]) # Use find_by_id to get nil instead of an error if user doesn't exist
#  end



end

and in task controller:

class TasksController < ApplicationController


  # GET /tasks
  # GET /tasks.xml
  def index
    @menu = "Timesheet"
    @page_name = "Manage Task"
    company_id = Company.find_by_subdomain(request.subdomain)
    @users = User.find_all_by_company_id(company_id)
    @tasks = current_user.tasks.all#Task.all
    @task = Task.new

    respond_to do |format|
      format.html # index.html.erb
      format.html # new.html.erb
      format.xml  { render :xml => @tasks }
    end
  end
end

and my error message i got:

NameError in TasksController#index

undefined local variable or method `current_user' for #<TasksController:0xfa7e638>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:02:15+00:00Added an answer on May 30, 2026 at 9:02 am

    Hi friends i have found the way to create current_user method without using any gem or plugin:

    In my application_helper.rb i did this :

    module ApplicationHelper
      def current_user
        User.find(session[:current_user_id])
      end
    end
    

    and at the end in my application controller.rb i called this, because from here i can access it through the application:

    class ApplicationController < ActionController::Base
      include UrlHelper 
      include ApplicationHelper
      helper_method :current_user
    end
    

    and now i can access any data related to current user:

    like :

    @tasks = current_user.tasks

    Thanks to all my friends for their valuable answers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know we can do correlated subqueries and join. But which one is faster?
I know you can not set a key value dynamically, but what about the
I know regex and substrings is a common question on here but i can
I know of cufon and one other similar. But what is the best way
Anyone know how to get the id of multiple placemarks each one created with
I know this question has been asked many times before but I can't find
I know that there has been one question about this but it is not
I know that you can use DirectoryIndex example.html in .htaccess, but what I need
I know some basic about iOS programming, now i want to connect my app
I know we can trigger an Intent.ACTION_SEND intent to send email. But according to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.