-
Notifications
You must be signed in to change notification settings - Fork 0
/
cancan1.sh
executable file
·167 lines (101 loc) · 3.76 KB
/
cancan1.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env bash
echo ~----------~----------Startingd $HOSTNAME, pwd: `pwd`, dlr0: "$0", bashsource0: "${BASH_SOURCE[0]}", $(date +"%Y-%m-%d_%H.%M.%S")
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# read settings..
read appn sfil sfil2 mpwd parm0</tmp/"_brvar1202_${USER}".txt
echo $appn $sfil $sfil2 $mpwd $parm0
# timeout1=15 ; read -t "${timeout1}" -p "Press ENTER or wait $timeout1 seconds..." || true; echo ;
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo 'gem "cancancan"' >> Gemfile
bundle
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# note .. roles table was created with users table in devise1.rb.
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rails g cancan:ability
### git. .. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
git add -A # Add all files and commit them
git commit -m "add cancan 1"
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# modify scaffold controller and users and roles controllers
# sub , add new lines of text after patrn...
r1tmp="/tmp/_temprubyrunner_${USER}.rb"
cat << 'HEREDOC' > $r1tmp
repl2 = %Q{
#cancancan
load_and_authorize_resource}
ARGF.each do |line|
puts line
puts repl2 if line =~ /before_action/
end
HEREDOC
filetarg='lib/templates/rails/scaffold_controller/controller.rb'
ruby $r1tmp $filetarg > $filetarg.tmp
cp $filetarg.tmp $filetarg; rm $filetarg.tmp
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# add to appl..contro..rb
r1tmp="/tmp/_temprubyrunner_${USER}.rb"
cat << 'HEREDOC' > $r1tmp
repl2 = %Q{
rescue_from CanCan::AccessDenied do |exception|
respond_to do |format|
format.json { head :forbidden }
format.html { redirect_to main_app.root_url, notice: exception.message }
end
end
}
ARGF.each do |line|
puts line
puts repl2 if line =~ /class ApplicationController/
end
HEREDOC
filetarg='app/controllers/application_controller.rb'
ruby $r1tmp $filetarg > $filetarg.tmp
cp $filetarg.tmp $filetarg; rm $filetarg.tmp
# Another exmple.
#
# rescue_from CanCan::AccessDenied do |exception|
# respond_to do |format|
# format.json { head :forbidden }
# format.html { redirect_to main_app.root_url, :alert => exception.message }
# end
# end
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# add to users_controller
r1tmp="/tmp/_temprubyrunner_${USER}.rb"
cat << 'HEREDOC' > $r1tmp
repl2 = %Q{
#cancancan
load_and_authorize_resource}
ARGF.each { |line| puts line ; puts repl2 if line =~ /before_action/ }
HEREDOC
filetarg='app/controllers/users_controller.rb'
if [ -f $filetarg ] ; then
ruby $r1tmp $filetarg > $filetarg.tmp ; cp $filetarg.tmp $filetarg ; rm $filetarg.tmp
else
echo 'skipping, file doesnt exist.'
fi
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# add to roles_controller
# r1tmp="/tmp/_temprubyrunner_${USER}.rb"
# cat << 'HEREDOC' > $r1tmp
# repl2 = %Q{
# #cancancan
# load_and_authorize_resource}
# ARGF.each { |line| puts line ; puts repl2 if line =~ /before_action/ }
# HEREDOC
# filetarg='app/controllers/roles_controller.rb'
# ruby $r1tmp $filetarg > $filetarg.tmp && cp $filetarg.tmp $filetarg && rm $filetarg.tmp
# add roles.
rails g scaffold Role name description active_status:integer sort:integer
# Add role to user.
rails g migration AddRoleToUser role:references
rake db:migrate
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# copy ability and user model..
mkdir -p backup/app/models
cp -a app/models/ backup/app/
cp -a $sfil2/app/models/ app/
# timeout1=15 ; read -t "${timeout1}" -p "Press ENTER or wait $timeout1 seconds..." || true; echo ;
### git. .. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
git add -A # Add all files and commit them
git commit -m "add cancan 2"