Commit 734ca0f2e857b85c9723e89ac5fb3bac7f883d28

  • avatar
  • Petri Huttunen <petri.huttunen @j…u.fi> (Committer)
  • Tue May 17 17:07:45 EEST 2011
  • avatar
  • Petri Huttunen <petri.huttunen @j…u.fi> (Author)
  • Tue May 17 17:07:45 EEST 2011
Commented helpers and models, fixed few comments.
app/controllers/groups_controller.rb
(1 / 1)
  
99
1010class GroupsController < ApplicationController
1111
12 # renders a list of groups.
12 # Renders a list of groups.
1313 def index
1414 @groups = Group.all
1515 end
app/controllers/pages_controller.rb
(2 / 2)
  
99
1010class PagesController < ApplicationController
1111
12 # Sets title as 'Home'
12 # Sets title as 'Home'.
1313 def index
1414 @title = "Home"
1515 end
1616
17 # Sets title as 'About'
17 # Sets title as 'About'.
1818 def about
1919 @title = "About"
2020 end
app/helpers/application_helper.rb
(1 / 0)
  
99
1010module ApplicationHelper
1111
12 # Changes the page title automatically, if it is defined.
1213 def title
1314 base_title = "YouData"
1415 if @title.nil?
app/helpers/project_helper.rb
(2 / 0)
  
88# Copyright (c) 2011 Olli Wirpi
99
1010module ProjectHelper
11
12 # Array mapping visibility choices from project model to cleartext definitions.
1113 def visibility_choices
1214 [
1315 ["Visible to everyone", Project::VISIBILITY_ALL],
app/helpers/sessions_helper.rb
(25 / 2)
  
99
1010module SessionsHelper
1111
12 # Creates a permanent cookie for the user for identification
13 # and signs him in to the system.
1214 def sign_in(user)
1315 cookies.permanent.signed[:remember_token] = [user.id, user.salt]
1416 self.current_user = user
1517 end
16
18
19 # Deletes the identification cookie from the user
20 # and signs him out of the system.
1721 def sign_out
1822 cookies.delete(:remember_token)
1923 self.current_user = nil
2024 end
2125
26 # Checks if user is signed in.
2227 def signed_in?
2328 !current_user.nil?
2429 end
2530
31 # Checks if user can view a project, which is visible
32 # only to logged in users.
2633 def visible_if_logged_in(project)
2734 !current_user.nil? && project.visibility == 2 #TODO: chech if collaborator
2835 end
2936
37 # Checks if datafile is visible to either logged in users
38 # or to everyone.
3039 def logged_in_or_public_file(datafile)
3140 datafile.project.visibility == 1 || visible_if_logged_in(datafile.project)
3241 end
33
42
43 # Checks if the specified user is the current user.
3444 def current_user?(user)
3545 user == current_user
3646 end
3747
48 # Assigns the specified user to the current user instance variable.
3849 def current_user=(user)
3950 @current_user = user
4051 end
4152
53 # Returns either the current user instance variable, or
54 # assigns the current user instance variable from
55 # the cookie and then returns it.
4256 def current_user
4357 @current_user ||= user_from_remember_token
4458 end
4559
60 # Redirects user to login page, storing the previous location
61 # for after login to redirect back to previous page.
4662 def deny_access
4763 store_location
4864 redirect_to login_path, :notice => "You need to be logged in to view this page"
4965 end
5066
67 # Redirects the user back to either the previous page or
68 # to the specified default page.
5169 def redirect_back_or(default)
5270 redirect_to(session[:return_to] || default)
5371 clear_return_to
7373
7474 private
7575
76 # Authenticates the user from the cookie.
7677 def user_from_remember_token
7778 User.authenticate_with_salt(*remember_token)
7879 end
7980
81 # Returns either the remember token used for authentication
82 # or empty token for non-logged in user.
8083 def remember_token
8184 cookies.signed[:remember_token] || [nil, nil]
8285 end
8386
87 # Stores the url for the user.
8488 def store_location
8589 session[:return_to] = request.fullpath
8690 end
8791
92 # Clears the stored url.
8893 def clear_return_to
8994 session[:return_to] = nil
9095 end
app/models/datafile.rb
(10 / 0)
  
2929
3030 references_one :anonymous_datas
3131
32 # Moves the datafile to another project,
33 # removing links to other datafiles.
3234 def change_project(target_project)
3335 # Remove all related files.
3436 if !datafiles.blank?
5353 referenced_in :datafile
5454end
5555
56# Attachment holds one file associated with
57# the datafile.
5658class Attachment
5759 include Mongoid::Document
5860 field :name, :type => String
6565
6666end
6767
68# AnonymousData is a class which describes
69# the metadata associated with an anonymously
70# uploaded datafile.
6871class AnonymousData
6972 include Mongoid::Document
7073 field :sender, :type => String
7777
7878 referenced_in :inbox
7979
80 # Removes all anonymously uploaded datafiles
81 # which have expired, i.e. are uploaded over
82 # 14 days ago.
8083 def self.remove_old
8184 self.all.each do |anondata|
8285 if 14.days.ago > anondata.uploadTime
app/models/group.rb
(6 / 0)
  
77# Copyright (c) 2011 Marko Malinen
88# Copyright (c) 2011 Olli Wirpi
99
10# Holds information about the group,
11# but always asks from Korppi about
12# the current group situation.
1013class Group
1114 include Mongoid::Document
1215
1919 references_many :users
2020 references_and_referenced_in_many :viewable_projects, :inverse_of => :viewer_groups
2121
22 # Asks Korppi if the user with specified
23 # username is part of the group. Currently
24 # nonfunctional.
2225 def member?(username)
2326 # ask Korppi if username belongs to group cn
2427 end
app/models/inbox.rb
(2 / 0)
  
77# Copyright (c) 2011 Marko Malinen
88# Copyright (c) 2011 Olli Wirpi
99
10# User's inbox holds the anonymously
11# uploaded data.
1012class Inbox
1113 include Mongoid::Document
1214
app/models/project.rb
(10 / 2)
  
77# Copyright (c) 2011 Marko Malinen
88# Copyright (c) 2011 Olli Wirpi
99
10# Project holds datafiles and metadata
11# about the rights for the project etc.
1012class Project
1113 include Mongoid::Document
1214
3232 validates :name, :presence => true
3333 validates :user_id, :presence => true
3434
35 # add collabs, now only works for the owner
35 # Checks if the specified user
36 # is a collaborator for the project.
37 # Currently doesn't work for others
38 # than the owner of the project.
3639 def collaborator?(candidate)
3740 self.user == candidate
3841 end
3942
43 # Returns true if the project is visible to all.
4044 def visibility_all?
4145 self.visibility == VISIBILITY_ALL
4246 end
4347
48 # Returns true if the project is visible to logged in users.
4449 def visibility_logged_in?
4550 self.visibility == VISIBILITY_LOGGED_IN
4651 end
4752
53 # Returns true if the project is visible to project collaborators.
4854 def visibility_collaborators?
4955 self.visibility == VISIBILITY_COLLABORATORS
5056 end
5157
52
58 # Returns true if the project can be viewed by the specified user.
5359 def can_be_viewed_by?(candidate)
5460 return true if self.visibility_all?
5561 return true if self.visibility_logged_in? && !candidate.nil?
app/models/user.rb
(8 / 0)
  
77# Copyright (c) 2011 Marko Malinen
88# Copyright (c) 2011 Olli Wirpi
99
10# User class has functions for the profile
11# and authentication of the user.
1012class User
1113 include Mongoid::Document
1214
3030 :format => { :with => email_regex },
3131 :uniqueness => { :case_sensitive => false }
3232
33 # Checks if the username and password are valid
34 # Korppi credentials.
3335 def self.authenticate(username, password)
3436 # Korppi-id here
3537 #ldap = Net::LDAP.new :host => "kldap.cc.jyu.fi", :port => 6363, :encryption => :simple_tls
4242 return true
4343 end
4444
45 # Finds the user based on the id and makes sure
46 # the cookie's salt is the same as the user's salt.
4547 def self.authenticate_with_salt(id, cookie_salt)
4648 user = User.find(id) unless id.nil?
4749 (user && user.salt == cookie_salt) ? user : nil
4850 end
4951
52 # Creates a salt for a new user.
5053 def add_salt(password)
5154 self.salt = secure_hash("#{Time.now.utc}--#{password}") if new_record?
5255 end
5356
5457 private
5558
59 # Creates a secure hash out of the string.
5660 def secure_hash(string)
5761 Digest::SHA2.hexdigest(string)
5862 end