Skip to content

Commit

Permalink
user_mailer specific stuff added/modified
Browse files Browse the repository at this point in the history
  • Loading branch information
bakkdoor committed Sep 3, 2008
1 parent b6dd9f3 commit eb90db2
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ class UsersController < ApplicationController
# before_filter :admin_required, :only => [:suspend, :unsuspend, :destroy, :purge]
before_filter :find_user, :only => [:suspend, :unsuspend, :destroy, :purge]

def index
@users = User.all
end

# render new.rhtml
def new
Expand Down
202 changes: 202 additions & 0 deletions src/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class User < ActiveRecord::Base
validates_format_of :email, :with => Authentication.email_regex, :message => Authentication.bad_email_message


has_many :project_memberships
has_many :projects, :through => :project_memberships
has_many :project_invitations


# HACK HACK HACK -- how to do attr_accessible from here?
# prevents a user from submitting a crafted form that bypasses activation
Expand Down Expand Up @@ -47,6 +51,204 @@ def email=(value)
write_attribute :email, (value ? value.downcase : nil)
end





# returns all projects which have changed since a given date_time
def changed_projects_since(date_time)
@projects = self.projects.select{|p| p.updated_at >= date_time}
end

def is_a_project_admin?
self.project_memberships.each do |m|
if m.user_level == User.level_code(:project_admin)
return true
end
end

false
end

# indicates, if a user can join a project
def can_join?(project)
(project.public && !project.invite_only) || (self.has_invitation_for?(project))
end

# joins a project, if possible
def join(project)
if self.can_join?(project)
membership = ProjectMembership.new(:project_id => project.id, :user_id => self.id, :user_level => User.level_code(:new_member))
membership.save

# search for project_invitation and delete it, if available
# if not => error, since something obviously went wrong!
if self.has_invitation_for?(project)
invitation = ProjectInvitation.find_by_user_id_and_project_id(self.id, project.id)

if invitation
invitation.destroy
else
raise ActiveRecord::RecordNotFound, "Invitation doesn't seem to exist. Something is wrong here!"
end

end
end
end

def can_invite_users_to_project?(project)
membership = ProjectMembership.find_by_user_id_and_project_id(self.id, project.id)
if membership
membership.user_level >= 80
else
false
end
end

# invite a user to a project
# if no message is given, a standard message will be sent.
def invite_user_to_project(user, project, message = nil)
if can_invite_users_to_project?(project)
message ||= ProjectInvitation.default_message(project.name, self.login)
if user && project
ProjectInvitation.create(:user_id => user.id, :project_id => project.id, :message => message)
else
logger.error "error while trying to invite user to project: user_id => #{user.id}, project_id => #{project.id}, message => #{message}"
end
end
end

# indicates, if the user has an invitation for a given project
def has_invitation_for?(project)
ProjectInvitation.find(:first, :conditions => ["user_id = ? AND project_id = ?", self.id, project.id])
end

# returns all received messages
def received_messages
Message.find(:all, :order => "created_at DESC", :conditions => ["receiver_id = ? AND receiver_deleted = ?", self.id, false])
end

# returns all sent messages
def sent_messages
Message.find(:all, :order => "created_at DESC", :conditions => ["author_id = ? AND author_deleted = ?", self.id, false])
end

# returns all unread (new) messages
def new_messages
Message.find(:all, :conditions => ["receiver_id = ? AND receiver_deleted = ? AND is_read = ?", self.id, false, false])
end

def amount_new_messages
self.new_messages.size
end

# indicates, if there are any new messages
def has_new_messages?
self.new_messages.size > 0
end

def can_delete_message?(message)
(message.author_id == self.id) || (message.receiver_id == self.id)
end

def delete_message(message)
if message.author_id == self.id
message.author_deleted = true
message.save
elsif message.receiver_id == self.id
message.receiver_deleted = true
message.save
end
end

def can_read_message?(message)
(message.author_id == self.id) || (message.receiver_id == self.id)
end

def read_message(message)
if can_read_message?(message) && message.receiver_id == self.id
message.is_read = true
message.save
end
end

# searching for users
def self.search(query)
if query
find(:all, :order => "login ASC", :conditions => ["login LIKE ?", "%#{query}%"])
else
find(:all)
end
end

# returns all public projects of a user
def public_projects
self.projects.select do |p|
p.public
end
end

# returns a user_level_code based on a given level_name-symbol
# for example: :admin => 100
def self.level_code(level_name)
case level_name
when :admin
100
when :moderator
90
when :project_admin
85
when :projectmanager
80
when :project_member
10
when :bugreporter
8
when :readonly
5
when :new_member
1
when :waitin_for_auth
0
else
#-1 # this is not valid, normally this shouldn't happen!
raise InvalidUserLevelError.new("Userlevel unknown: '#{level_name}'")
end
end

# returns a string based on a given user_level_code
# for example: 100 -> "admin"
def self.level_name(level_code)
case level_code
when 100
"Admin"
when 90
"Moderator"
when 85
"Project-Admin"
when 80
"Project-Manager"
when 10
"Project member"
when 8
"Bugreporter"
when 5
"Readonly"
when 1
"New member"
when 0
"Waiting for authorization"
when -1
"No rights at all!" # corresponds to 0
else
"Unknown" # normally this shouldn't happen!
end
end





protected

def make_activation_code
Expand Down
15 changes: 13 additions & 2 deletions src/app/models/user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,22 @@ def activation(user)
@body[:url] = "http://YOURSITE/"
end

def forgot_password(user)
setup_email(user)
@subject += 'You have requested to change your password'
@body[:url] = "http://localhost:3000/reset_password/#{user.password_reset_code}"
end

def reset_password(user)
setup_email(user)
@subject += 'Your password has been reset.'
end

protected
def setup_email(user)
@recipients = "#{user.email}"
@from = "ADMINEMAIL"
@subject = "[YOURSITE] "
@from = "users@localhost"
@subject = "[users@YARPS] "
@sent_on = Time.now
@body[:user] = user
end
Expand Down
14 changes: 8 additions & 6 deletions src/app/models/user_observer.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
class UserObserver < ActiveRecord::Observer
def after_create(user)
#user.reload
UserMailer.deliver_signup_notification(user)
end

def after_save(user)

UserMailer.deliver_activation(user) if user.recently_activated?


def after_save(user)
#user.reload
UserMailer.deliver_activation(user) if user.pending?
#UserMailer.deliver_forgot_password(user) if user.recently_forgot_password?
#UserMailer.deliver_reset_password(user) if user.recently_reset_password?
end
end
end
File renamed without changes.
10 changes: 10 additions & 0 deletions src/config/initializers/mail.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Email settings
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "mail.yourapplication.com",
:port => 25,
:domain => "yourapplication.com",
:authentication => :login,
:user_name => "mail@yourapplication.com",
:password => "yourapplicationpassword"
}
6 changes: 5 additions & 1 deletion src/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
t.datetime "created_at"
end

add_index "taggings", ["taggable_id", "taggable_type"], :name => "index_taggings_on_taggable_id_and_taggable_type"
add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id"
add_index "taggings", ["taggable_id", "taggable_type"], :name => "index_taggings_on_taggable_id_and_taggable_type"

create_table "tags", :force => true do |t|
t.string "name"
Expand All @@ -77,6 +77,10 @@
t.datetime "activated_at"
t.string "state", :default => "passive"
t.datetime "deleted_at"
t.datetime "last_login"
t.integer "failed_logins"
t.integer "user_level", :default => 1
t.string "language", :default => "en"
end

add_index "users", ["login"], :name => "index_users_on_login", :unique => true
Expand Down

0 comments on commit eb90db2

Please sign in to comment.