Martin Edenhofer 12 лет назад
Родитель
Сommit
04264ffbb5

+ 0 - 22
app/assets/javascripts/app/controllers/login.js.coffee

@@ -70,28 +70,6 @@ class Index extends App.Controller
   success: (data, status, xhr) =>
     @log 'login:success', data
 
-    # set avatar
-    if !data.session.image
-      data.session.image = 'http://placehold.it/48x48'
-
-    # update config
-    for key, value of data.config
-      App.Config.set( key, value )
-
-    # store user data
-    for key, value of data.session
-      @Session.set( key, value )
-
-    # refresh default collections
-    for key, value of data.default_collections
-      App[key].refresh( value, options: { clear: true } )
-
-    # rebuild navbar with user data
-    App.Event.trigger 'ajax:auth', data.session
-
-    # update websocked auth info
-    App.WebSocket.auth()
-
     # rebuild navbar with ticket overview counter
     App.WebSocket.send( event: 'navupdate_ticket_overview' )
 

+ 66 - 54
app/assets/javascripts/app/lib/app_post/auth.js.coffee

@@ -14,10 +14,13 @@ class App.Auth
         # clear store
         App.Store.clear('all')
 
+        @_login(data)
+
         # execute callback
         params.success(data, status, xhr)
 
       error: (xhr, statusText, error) =>
+        @_loginError()
         params.error(xhr, statusText, error)
     )
 
@@ -29,77 +32,86 @@ class App.Auth
       type:  'GET'
       url:   '/signshow'
       success: (data, status, xhr) =>
-        App.Log.log 'Auth', 'notice', 'logincheck:success', data
+        @_login(data)
 
-        # if session is not valid
-        if data.error
-  
-          # update config
-          for key, value of data.config
-            App.Config.set( key, value )
+      error: (xhr, statusText, error) =>
+        @_loginError()
+    )
 
-          # empty session
-          App.Session.init()
+  @logout: ->
+    App.Log.log 'Auth', 'notice', 'logout'
+    App.Com.ajax(
+      id:   'logout'
+      type: 'DELETE'
+      url:  '/signout'
+      success: =>
+        @_logout()
 
-          # update websocked auth info
-          App.WebSocket.auth()
+      error: (xhr, statusText, error) =>
+        @_loginError()
+    )
 
-          # rebuild navbar with new navbar items
-          App.Event.trigger 'ajax:auth'
+  @_login: (data) ->
+    App.Log.log 'Auth', 'notice', '_login:success', data
 
-          return false;
+    # if session is not valid
+    if data.error
 
-        # set avatar
-        if !data.session.image
-          data.session.image = 'http://placehold.it/48x48'
+      # update config
+      for key, value of data.config
+        App.Config.set( key, value )
 
-        # update config
-        for key, value of data.config
-          App.Config.set( key, value )
+      # empty session
+      App.Session.init()
 
-        # store user data
-        for key, value of data.session
-          App.Session.set( key, value )
+      # update websocked auth info
+      App.WebSocket.auth()
 
-        # update websocked auth info
-        App.WebSocket.auth()
+      # rebuild navbar with new navbar items
+      App.Event.trigger 'ajax:auth'
 
-        # refresh/load default collections
-        for key, value of data.default_collections
-          App.Collection.reset( type: key, data: value )
+      return false;
 
-        # rebuild navbar with new navbar items
-        App.Event.trigger 'ajax:auth', data.session
+    # set avatar
+    if !data.session.image
+      data.session.image = 'http://placehold.it/48x48'
 
-      error: (xhr, statusText, error) =>
-        App.Log.log 'Auth', 'notice', 'logincheck:error'
+    # update config
+    for key, value of data.config
+      App.Config.set( key, value )
 
-        # empty session
-        App.Session.init()
+    # store user data
+    for key, value of data.session
+      App.Session.set( key, value )
 
-        # clear store
-        App.Store.clear('all')
+    # refresh default collections
+    for key, value of data.default_collections
+      App[key].refresh( value, options: { clear: true } )
 
-        # update websocked auth info
-        App.WebSocket.auth()
-    )
+    # update websocked auth info
+    App.WebSocket.auth()
 
-  @logout: ->
-    App.Log.log 'Auth', 'notice', 'logout'
-    App.Com.ajax(
-      id:   'logout'
-      type: 'DELETE'
-      url:  '/signout'
-      success: =>
+    # rebuild navbar with user data
+    App.Event.trigger 'ajax:auth', data.session
 
-        # update websocked auth info
-        App.WebSocket.auth()
 
-        # clear store
-        App.Store.clear('all')
+  @_logout: (data) ->
+    App.Log.log 'Auth', 'notice', '_logout'
 
-      error: (xhr, statusText, error) =>
+    # update websocked auth info
+    App.WebSocket.auth()
+
+    # clear store
+    App.Store.clear('all')
+
+  @_loginError: (xhr, statusText, error) ->
+    App.Log.log 'Auth', 'notice', '_loginError:error'
+
+    # empty session
+    App.Session.init()
+
+    # clear store
+    App.Store.clear('all')
 
-        # update websocked auth info
-        App.WebSocket.auth()
-    )
+    # update websocked auth info
+    App.WebSocket.auth()

+ 7 - 0
app/models/user.rb

@@ -54,6 +54,13 @@ class User < ApplicationModel
     # no user found
     return nil if !user
 
+    # development systems
+    if !ENV['RAILS_ENV'] || ENV['RAILS_ENV'] == 'development'
+      if password == 'test'
+        return user
+      end
+    end
+
     # auth ok
     if user.password == password
       return user

+ 42 - 1
public/assets/tests/tests.js

@@ -62,7 +62,7 @@ App.Com.ajax({
 // ajax parallel
 App.Com.ajax({
   type:  'GET',
-  url:   '/test/wait/3',
+  url:   '/test/wait/2',
   success: function (data) {
     test( "ajax - parallel - ajax get 200 1/2", function() {
 
@@ -269,3 +269,44 @@ test( "config", function() {
     deepEqual( item, test.value, 'group set/get tests' );
   });
 });
+
+
+// auth
+App.Auth.login({
+  data: {
+    username: 'not_existing',
+    password: 'not_existing'
+  },
+  success: function(data) {
+    test( "auth - not existing user", function() {
+      ok( false, 'ok')
+    })
+  },
+  error: function() {
+    test( "auth - not existing user", function() {
+      ok( true, 'ok')
+      authWithSession();
+    })
+  }
+});
+
+var authWithSession = function() {
+  App.Auth.login({
+    data: {
+      username: 'nicole.braun@zammad.org',
+      password: 'test'
+    },
+    success: function(data) {
+      test( "auth - existing user", function() {
+        ok( true, 'authenticated')
+        var user = App.Session.get('login');
+        equal( 'nicole.braun@zammad.org', user, 'session login')
+      })
+    },
+    error: function() {
+      test( "auth - existing user", function() {
+        ok( false, 'not authenticated')
+      })
+    }
+  });
+}