Class: GroupController

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

Instance Method Summary collapse

Instance Method Details

#add_memberObject



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

def add_member
  case request.method
  when :post
    if ( group = Group.find(:first,:conditions=>["name=?",params[:id]]) )
      user = (=session[:user]) && User.()
      if user.super_user || group.owner == user
        unames = params[:user].split(/[;, ]+/)
        res, mesg = group.add_members(unames)
        flash[:notice] = mesg
      end
    end
    redirect_back_or_default :action => "list"
  when :get
    @group = Group.find(:first,:conditions=>["name=?",params[:id]])
  end
end

#change_ownerObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/controllers/group_controller.rb', line 76

def change_owner
  case request.method
  when :post
    if ( group = Group.find(:first,:conditions=>["name=?",params[:id]]) ) && ( owner = User.find(:first,:conditions=>["login=?",(oname=params[:user])]) )
      user = (=session[:user]) && User.()
      if user.super_user || group.owner == user
        group.owner = owner
        if group.respond_to?(:update) ? group.update : group.save
          flash[:notice] = "owner was changed to #{oname}"
        else
          flash[:notice] = "faild to change owner"
        end
      end
    end
    redirect_back_or_default :action => "list"
  when :get
    if (group = Group.find(:first,:conditions=>["name=?",params[:id]]))
      @name = group.name
      members = group.members
      root = User.find_by_sql("SELECT * FROM users WHERE login='root' LIMIT 1")[0]
      members.push(root) unless members.include?(root)
      members.delete(group.owner)
      @members = members.collect{|u| u.}
    end
  end
end

#createObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/controllers/group_controller.rb', line 6

def create
  user = (=session[:user]) && User.()
  raise("only super user can create groups") if !User.super_user?(user)

  case request.method
  when :post
    if user 
      group = Group.new(:name => params[:group][:name])
      group.owner = user
      if group.save
        flash['notice']  = "Create group successful"
        redirect_back_or_default :action => "list"
        return
      end
    end
    flash['notice']  = "Failed to create a group"
    redirect_back_or_default :action => "list"
  end
end

#deleteObject



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

def delete
  user = (=session[:user]) && User.()
  name = params[:id]
  group = Group.find(:first,:conditions=>["name=?",name])
  if group
    if group.owner == user || User.super_user?(user)
      if group.destroy
        flash[:notice] = "delete group successed #{name}"
      end
    end
  end
  redirect_back_or_default :action => "list"
end

#delete_memberObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/group_controller.rb', line 57

def delete_member
  case request.method
  when :post
    if ( group = Group.find(:first,:conditions=>["name=?",params[:id]]) )
      user = (=session[:user]) && User.()
      if user.super_user || group.owner == user
        res, mesg = group.del_member(params[:user])
        flash[:notice] = mesg
      end
    end
    redirect_back_or_default :action => "list"
  when :get
    if (group = Group.find(:first,:condition=>["name=?",params[:id]]))
      @name = group.name
      @members = group.members.collect{|u| u.}
    end
  end
end

#listObject



103
104
105
106
107
108
109
110
111
112
113
# File 'app/controllers/group_controller.rb', line 103

def list
  user = (=session[:user]) && User.()
  if User.super_user?(user)
    @own_groups = Group.find(:all)
    @super_user = true
  else
    @own_groups = user.own_groups(true)
    @super_user = false
  end
  @belonging_groups = user.belonging_groups
end