diff

Patch for activating REST API for users - Cyril Servant, 2010-09-28 13:43

Download (9.62 KB)

 
1
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
2
index b854850..9bac8a7 100644
3
--- a/app/controllers/users_controller.rb
4
+++ b/app/controllers/users_controller.rb
5
@@ -65,13 +65,16 @@ class UsersController < ApplicationController
6
         return
7
       end
8
     end
9
-    render :layout => 'base'
10
+    respond_to do |format|
11
+      format.html { render :layout => 'base' }
12
+      format.xml { }
13
+    end
14
 
15
   rescue ActiveRecord::RecordNotFound
16
     render_404
17
   end
18
 
19
-  def add
20
+  def create
21
     if request.get?
22
       @user = User.new(:language => Setting.default_language)
23
     else
24
@@ -81,16 +84,24 @@ class UsersController < ApplicationController
25
       @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless @user.auth_source_id
26
       if @user.save
27
         Mailer.deliver_account_information(@user, params[:password]) if params[:send_information]
28
-        flash[:notice] = l(:notice_successful_create)
29
-        redirect_to(params[:continue] ? {:controller => 'users', :action => 'add'} : 
30
-                                        {:controller => 'users', :action => 'edit', :id => @user})
31
+        respond_to do |format|
32
+          format.html {
33
+            flash[:notice] = l(:notice_successful_create)
34
+            redirect_to(params[:continue] ? {:controller => 'users', :action => 'add'} : 
35
+                                            {:controller => 'users', :action => 'update', :id => @user})
36
+          }
37
+          format.xml  { head :created, :location => url_for(:controller => 'users', :action => 'show', :id => @user.id) }
38
+        end
39
         return
40
       end
41
     end
42
-    @auth_sources = AuthSource.find(:all)
43
+    respond_to do |format|
44
+      format.html { @auth_sources = AuthSource.find(:all) }
45
+      format.xml  { render :xml => @project.errors, :status => :unprocessable_entity }
46
+    end
47
   end
48
 
49
-  def edit
50
+  def update
51
     @user = User.find(params[:id])
52
     if request.post?
53
       @user.admin = params[:user][:admin] if params[:user][:admin]
54
@@ -112,10 +123,31 @@ class UsersController < ApplicationController
55
         redirect_to :back
56
       end
57
     end
58
+    if request.put?
59
+      @user.admin = params[:user][:admin] if params[:user][:admin]
60
+      @user.login = params[:user][:login] if params[:user][:login]
61
+      if params[:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?)
62
+        @user.password, @user.password_confirmation = params[:password], params[:password_confirmation]
63
+      end
64
+      @user.group_ids = params[:user][:group_ids] if params[:user][:group_ids]
65
+      @user.attributes = params[:user]
66
+      # Was the account actived ? (do it before User#save clears the change)
67
+      was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
68
+      if @user.save
69
+        if was_activated
70
+          Mailer.deliver_account_activated(@user)
71
+        elsif @user.active? && params[:send_information] && !params[:password].blank? && @user.auth_source_id.nil?
72
+          Mailer.deliver_account_information(@user, params[:password])
73
+        end
74
+        head :ok
75
+      else
76
+        render :xml => @user.errors, :status => :unprocessable_entity
77
+      end
78
+    end
79
     @auth_sources = AuthSource.find(:all)
80
     @membership ||= Member.new
81
   rescue ::ActionController::RedirectBackError
82
-    redirect_to :controller => 'users', :action => 'edit', :id => @user
83
+    redirect_to :controller => 'users', :action => 'update', :id => @user
84
   end
85
   
86
   def edit_membership
87
diff --git a/app/views/users/add.rhtml b/app/views/users/add.rhtml
88
deleted file mode 100644
89
index 2e0743e..0000000
90
--- a/app/views/users/add.rhtml
91
+++ /dev/null
92
@@ -1,10 +0,0 @@
93
-<h2><%= link_to l(:label_user_plural), :controller => 'users', :action => 'index' %> &#187; <%=l(:label_user_new)%></h2>
94
-
95
-<% labelled_tabular_form_for :user, @user, :url => { :action => "add" }, :html => { :class => nil } do |f| %>
96
-	<%= render :partial => 'form', :locals => { :f => f } %>
97
-	<p><label><%= check_box_tag 'send_information', 1, true %> <%= l(:label_send_information) %></label></p>
98
-	<p>
99
-		<%= submit_tag l(:button_create) %>
100
-		<%= submit_tag l(:button_create_and_continue), :name => 'continue' %>
101
-	</p>
102
-<% end %>
103
diff --git a/app/views/users/create.rhtml b/app/views/users/create.rhtml
104
new file mode 100644
105
index 0000000..0e7a333
106
--- /dev/null
107
+++ b/app/views/users/create.rhtml
108
@@ -0,0 +1,10 @@
109
+<h2><%= link_to l(:label_user_plural), :controller => 'users', :action => 'index' %> &#187; <%=l(:label_user_new)%></h2>
110
+
111
+<% labelled_tabular_form_for :user, @user, :url => { :action => "create" }, :html => { :class => nil } do |f| %>
112
+	<%= render :partial => 'form', :locals => { :f => f } %>
113
+	<p><label><%= check_box_tag 'send_information', 1, true %> <%= l(:label_send_information) %></label></p>
114
+	<p>
115
+		<%= submit_tag l(:button_create) %>
116
+		<%= submit_tag l(:button_create_and_continue), :name => 'continue' %>
117
+	</p>
118
+<% end %>
119
diff --git a/app/views/users/edit.rhtml b/app/views/users/edit.rhtml
120
deleted file mode 100644
121
index f5538c1..0000000
122
--- a/app/views/users/edit.rhtml
123
+++ /dev/null
124
@@ -1,10 +0,0 @@
125
-<div class="contextual">
126
-<%= link_to l(:label_profile), {:controller => 'users', :action => 'show', :id => @user}, :class => 'icon icon-user' %>
127
-<%= change_status_link(@user) %>
128
-</div>
129
-
130
-<h2><%= link_to l(:label_user_plural), :controller => 'users', :action => 'index' %> &#187; <%=h @user.login %></h2>
131
-
132
-<%= render_tabs user_settings_tabs %>
133
-
134
-<% html_title(l(:label_user), @user.login, l(:label_administration)) -%>
135
diff --git a/app/views/users/index.rhtml b/app/views/users/index.rhtml
136
index 1b47028..e95bb8b 100644
137
--- a/app/views/users/index.rhtml
138
+++ b/app/views/users/index.rhtml
139
@@ -1,5 +1,5 @@
140
 <div class="contextual">
141
-<%= link_to l(:label_user_new), {:action => 'add'}, :class => 'icon icon-add' %>
142
+<%= link_to l(:label_user_new), {:action => 'create'}, :class => 'icon icon-add' %>
143
 </div>
144
 
145
 <h2><%=l(:label_user_plural)%></h2>
146
diff --git a/app/views/users/index.xml.builder b/app/views/users/index.xml.builder
147
new file mode 100644
148
index 0000000..6b3e977
149
--- /dev/null
150
+++ b/app/views/users/index.xml.builder
151
@@ -0,0 +1,16 @@
152
+xml.instruct!
153
+xml.users :type => 'array' do
154
+  @users.each do |user|
155
+    xml.user do
156
+      xml.id user.id
157
+      xml.login user.login
158
+      xml.firstname user.firstname
159
+      xml.lastname user.lastname
160
+      xml.mail user.mail
161
+      xml.admin user.admin
162
+      xml.created_on user.created_on
163
+      xml.last_login_on user.last_login_on
164
+    end
165
+  end
166
+end
167
+
168
diff --git a/app/views/users/show.xml.builder b/app/views/users/show.xml.builder
169
new file mode 100644
170
index 0000000..8f05fb5
171
--- /dev/null
172
+++ b/app/views/users/show.xml.builder
173
@@ -0,0 +1,17 @@
174
+xml.instruct!
175
+xml.user do
176
+  xml.id @user.id
177
+  xml.login @user.login
178
+  xml.firstname @user.firstname
179
+  xml.lastname @user.lastname
180
+  xml.mail @user.mail
181
+  xml.admin @user.admin
182
+  xml.created_on @user.created_on
183
+  xml.last_login_on @user.last_login_on
184
+  xml.projects do
185
+    @memberships.each do |membership|
186
+      xml.project :id => membership.id, :name => membership.project, :role => membership.roles.sort.collect(&:to_s).join(', '), :created_on => membership.created_on
187
+    end
188
+  end 
189
+end
190
+
191
diff --git a/app/views/users/update.rhtml b/app/views/users/update.rhtml
192
new file mode 100644
193
index 0000000..f5538c1
194
--- /dev/null
195
+++ b/app/views/users/update.rhtml
196
@@ -0,0 +1,10 @@
197
+<div class="contextual">
198
+<%= link_to l(:label_profile), {:controller => 'users', :action => 'show', :id => @user}, :class => 'icon icon-user' %>
199
+<%= change_status_link(@user) %>
200
+</div>
201
+
202
+<h2><%= link_to l(:label_user_plural), :controller => 'users', :action => 'index' %> &#187; <%=h @user.login %></h2>
203
+
204
+<%= render_tabs user_settings_tabs %>
205
+
206
+<% html_title(l(:label_user), @user.login, l(:label_administration)) -%>
207
diff --git a/config/routes.rb b/config/routes.rb
208
index eab2157..a746211 100644
209
--- a/config/routes.rb
210
+++ b/config/routes.rb
211
@@ -143,17 +143,18 @@ ActionController::Routing::Routes.draw do |map|
212
     users.with_options :conditions => {:method => :get} do |user_views|
213
       user_views.connect 'users', :action => 'index'
214
       user_views.connect 'users/:id', :action => 'show', :id => /\d+/
215
-      user_views.connect 'users/new', :action => 'add'
216
-      user_views.connect 'users/:id/edit/:tab', :action => 'edit', :tab => nil
217
+      user_views.connect 'users/new', :action => 'create'
218
+      user_views.connect 'users/:id/edit/:tab', :action => 'update', :tab => nil
219
     end
220
     users.with_options :conditions => {:method => :post} do |user_actions|
221
       user_actions.connect 'users', :action => 'add'
222
-      user_actions.connect 'users/new', :action => 'add'
223
-      user_actions.connect 'users/:id/edit', :action => 'edit'
224
+      user_actions.connect 'users/new', :action => 'create'
225
+      user_actions.connect 'users/:id/edit', :action => 'update'
226
       user_actions.connect 'users/:id/memberships', :action => 'edit_membership'
227
       user_actions.connect 'users/:id/memberships/:membership_id', :action => 'edit_membership'
228
       user_actions.connect 'users/:id/memberships/:membership_id/destroy', :action => 'destroy_membership'
229
     end
230
+    users.connect 'users/:id.:format', :action => 'update', :conditions => {:method => :put}
231
   end
232
 
233
   # For nice "roadmap" in the url for the index action
234
@@ -178,6 +179,9 @@ ActionController::Routing::Routes.draw do |map|
235
     project.resources :news, :shallow => true
236
   end
237
 
238
+  map.resources :users, :member => { } do |user|
239
+  end
240
+
241
   # Destroy uses a get request to prompt the user before the actual DELETE request
242
   map.project_destroy_confirm 'projects/:id/destroy', :controller => 'projects', :action => 'destroy', :conditions => {:method => :get}