Browse Source

Added password change to profile page.

Martin Edenhofer 12 years ago
parent
commit
d1b17ba9cc

+ 5 - 4
app/assets/javascripts/app/controllers/_application_controller_form.js.coffee

@@ -37,10 +37,11 @@ class App.ControllerForm extends App.Controller
               attribute.value = @params[attribute.name]
 
           # rename display and name to _confirm
-          attribute.display = attribute.display + ' (confirm)'
-          attribute.name = attribute.name + '_confirm';
-          item = @formGenItem( attribute, @model.className, fieldset )
-          item.appendTo(fieldset)
+          if !attribute.single
+            attribute.display = attribute.display + ' (confirm)'
+            attribute.name = attribute.name + '_confirm';
+            item = @formGenItem( attribute, @model.className, fieldset )
+            item.appendTo(fieldset)
 
     # return form
     return fieldset

+ 61 - 0
app/assets/javascripts/app/controllers/_profile/password.js.coffee

@@ -0,0 +1,61 @@
+class App.ProfilePassword extends App.Controller
+  events:
+    'submit form': 'update'
+
+  constructor: ->
+    super
+    return if !@authenticate()
+    @render()
+
+  render: =>
+
+    # item
+    html = $( App.view('profile/password')() )
+
+    configure_attributes = [
+      { name: 'password_old', display: 'Current Password', tag: 'input', type: 'password', limit: 100, null: false, class: 'input span4', single: true  },
+      { name: 'password_new', display: 'New Password',     tag: 'input', type: 'password', limit: 100, null: false, class: 'input span4',  },
+    ]
+
+    @form = new App.ControllerForm(
+      el:        html.find('.password_item')
+      model:     { configure_attributes: configure_attributes }
+      autofocus: false
+    )
+    @html html
+
+  update: (e) =>
+    e.preventDefault()
+    params = @formParam(e.target)
+    error = @form.validate(params)
+    if error
+      @formValidate( form: e.target, errors: error )
+      return false
+
+    @formDisable(e)
+
+    # get data
+    App.Com.ajax(
+      id:   'password_reset'
+      type: 'POST'
+      url:  'api/users/password_change'
+      data: JSON.stringify(params)
+      processData: true
+      success: @success
+      error:   @error
+    )
+
+  success: (data, status, xhr) =>
+    @render()
+    @notify(
+      type: 'success'
+      msg:  App.i18n.translateContent( 'Password changed successfully!' )
+    )
+
+  error: (xhr, status, error) =>
+    @render()
+    data = JSON.parse( xhr.responseText )
+    @notify(
+      type: 'error'
+      msg:  App.i18n.translateContent( data.message )
+    )

+ 1 - 3
app/assets/javascripts/app/controllers/_settings/area.js.coffee

@@ -1,5 +1,3 @@
-$ = jQuery.sub()
-
 class App.SettingsArea extends App.Controller
   constructor: ->
     super
@@ -23,7 +21,7 @@ class App.SettingsArea extends App.Controller
 
 class App.SettingsAreaItem extends App.Controller
   events:
-    'submit form': 'update',
+    'submit form': 'update'
 
   constructor: ->
     super

+ 22 - 11
app/assets/javascripts/app/controllers/profile.js.coffee

@@ -1,21 +1,32 @@
-class Index extends App.Controller
-#  events:
-#    'focusin [data-type=edit]':     'edit_in'
+class Index extends App.ControllerLevel2
+  toggleable: false
+#  toggleable: true
 
   constructor: ->
     super
-    
-    # set title
-    @title 'Profile'
 
+    return if !@authenticate()
+
+    @menu = [
+      { name: 'Password',       'target': 'password', controller: App.ProfilePassword, params: {} },
+      { name: 'Language',       'target': 'language', controller: App.ProfileLinkedAccounts, params: { area: 'Ticket::Number' } },
+      { name: 'Link Accounts',  'target': 'accounts', controller: App.ProfileLinkedAccounts, params: { area: 'Ticket::Number' } },
+#      { name: 'Notifications',  'target': 'notify',   controller: App.SettingsArea, params: { area: 'Ticket::Number' } },
+    ] 
+    @page = {
+      title:     'Profile',
+      head:      'Profile',
+      sub_title: 'Settings'
+      nav:       '#profile',
+    }
+
+    # render page
     @render()
-    
-    @navupdate '#profile'
 
-    
-  render: ->
-    @html App.view('profile')()
+#  render: ->
+#    @html App.view('profile')()
 
 
+App.Config.set( 'profile/:target', Index, 'Routes' )
 App.Config.set( 'profile', Index, 'Routes' )
 App.Config.set( 'Profile', { prio: 1700, parent: '#current_user', name: 'Profile', target: '#profile', role: [ 'Agent', 'Customer' ] }, 'NavBarRight' )

+ 0 - 2
app/assets/javascripts/app/controllers/reset_password.js.coffee

@@ -1,5 +1,3 @@
-$ = jQuery.sub()
-
 class Index extends App.Controller
   className: 'container'
 

+ 0 - 2
app/assets/javascripts/app/controllers/settings.js.coffee

@@ -1,5 +1,3 @@
-$ = jQuery.sub()
-
 class Index extends App.ControllerLevel2
   toggleable: false
 #  toggleable: true

+ 0 - 2
app/assets/javascripts/app/views/profile.jst.eco

@@ -1,14 +1,12 @@
 <div class="page-header">
   <h1><%- @T( 'Profile' ) %><small></small></h1>
 </div>
-<!--
 <ul>
   <li><%- @T( 'Password' ) %></li>
   <li><%- @T( 'Link Accounts' ) %></li>
   <li><%- @T( 'Notifications' ) %></li>
   <li></li>
 </ul>
--->
     <div class="container">
       <div class="row">
         <div class="span12">

+ 7 - 0
app/assets/javascripts/app/views/profile/password.jst.eco

@@ -0,0 +1,7 @@
+<form class="">
+  <h2><%- @T( 'Password' ) %></h2>
+  <p><%- @T( 'Change your password.' ) %></p>
+  <div class="password_item"></div>
+  <button type="submit" class="btn"><%- @T( 'Submit' ) %></button>
+</form>
+<hr/>

+ 43 - 0
app/controllers/users_controller.rb

@@ -375,4 +375,47 @@ curl http://localhost/api/users/password_reset_verify.json -v -u #{login}:#{pass
     end
   end
 
+=begin
+
+Resource:
+POST /api/users/password_change
+
+Payload:
+{
+  "password_old": "some_password_old",
+  "password_new" "some_password_new"
+}
+
+Response:
+{
+  :message => 'ok'
+}
+
+Test:
+curl http://localhost/api/users/password_change.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X POST -d '{"password_old": "password_old", "password_new" "password_new"}'
+
+=end
+
+  def password_change
+
+    # check old password
+    if !params[:password_old]
+      render :json => { :message => 'Old password needed!' }, :status => :unprocessable_entity
+      return  
+    end
+    user = User.authenticate( current_user.login, params[:password_old] )
+    if !user
+      render :json => { :message => 'Old password is wrong!' }, :status => :unprocessable_entity
+      return  
+    end
+
+    # set new password
+    if !params[:password_new]
+      render :json => { :message => 'New password needed!' }, :status => :unprocessable_entity
+      return  
+    end
+    user.update_attributes( :password => params[:password_new] )
+    render :json => { :message => 'ok', :user_login => user.login }, :status => :ok
+  end
+
 end

+ 7 - 6
config/routes/user.rb

@@ -2,13 +2,14 @@ module ExtraRoutes
   def add(map)
 
     # users
-    map.match '/api/users/search',                :to => 'users#search', :via => [:get, :post]
-    map.match '/api/users/password_reset',        :to => 'users#password_reset_send', :via => :post
+    map.match '/api/users/search',                :to => 'users#search',                :via => [:get, :post]
+    map.match '/api/users/password_reset',        :to => 'users#password_reset_send',   :via => :post
     map.match '/api/users/password_reset_verify', :to => 'users#password_reset_verify', :via => :post
-    map.match '/api/users',                       :to => 'users#index',  :via => :get
-    map.match '/api/users/:id',                   :to => 'users#show',   :via => :get
-    map.match '/api/users',                       :to => 'users#create', :via => :post
-    map.match '/api/users/:id',                   :to => 'users#update', :via => :put
+    map.match '/api/users/password_change',       :to => 'users#password_change',       :via => :post
+    map.match '/api/users',                       :to => 'users#index',                 :via => :get
+    map.match '/api/users/:id',                   :to => 'users#show',                  :via => :get
+    map.match '/api/users',                       :to => 'users#create',                :via => :post
+    map.match '/api/users/:id',                   :to => 'users#update',                :via => :put
 
   end
   module_function :add

Some files were not shown because too many files changed in this diff