Index: domain_bonus_login_restrict.module
===================================================================
--- domain_bonus_login_restrict.module	(revision 1504)
+++ domain_bonus_login_restrict.module	(revision 1502)
@@ -19,13 +19,7 @@
  *
  */
 function domain_bonus_login_restrict_form_alter(&$form, $form_state, $form_id) {
-  if($form_id == 'user_login') {
-    $form['#validate'] = array_merge(
-      array_slice($form['#validate'], 0, count($form['#validate']) - 2),
-      array('domain_bonus_login_restrict_login_validate'),
-      array_slice($form['#validate'], count($form['#validate']) - 2)
-    );
-  }
+
   //add form element on domain_configure_form
   if ($form_id == 'domain_configure_form') {
 
@@ -51,20 +45,37 @@
     
     return $form;
   }
-}
- 
-function domain_bonus_login_restrict_login_validate($form, &$form_state) {
-  global $user;
-  global $_domain;
+} 
+
+/**
+ * Implementation of hook_user()
+ *
+ */
+function domain_bonus_login_restrict_user($op, &$edit, &$account, $category = NULL) {
 
-  $result = db_query('SELECT de.domain_id FROM {domain_editor} de INNER JOIN {users} u ON u.uid = de.uid WHERE u.name="%s"', $form_state['values']['name']);
+  if ($op == 'login') {
 
-  $user_domains = array();
-  while($domain = db_fetch_object($result)) {
-    $user_domains[$domain->domain_id] = TRUE;
-  }
-  // if user doesn't belong to this domain
-  if (!$user_domains[$_domain['domain_id']]) {
-    form_set_error('name', t('The account you tried to login with does not have access to this domain'));
+    // if set make user check by domain affiliation 
+    if (variable_get('domain_bonus_login_restrict', 0) && !user_access('login to any domain')) {
+      global $user;
+      global $_domain;
+      
+      $user_domains = domain_get_user_domains($user);
+
+      // user_domains retruns [-1] = -1 for current domain
+      // make it in proper way 
+      if ($user_domains[-1] == -1) {
+        $user_domains[0] = TRUE;
+      }
+
+      // if user doesn't belong to this domain
+      if (!$user_domains[$_domain['domain_id']]) {
+        // logout this user
+        session_destroy();
+        module_invoke_all('user', 'logout', NULL, $user);
+        // Load the anonymous user
+        $user = drupal_anonymous_user();
+      }
+    }
   }
-}
\ No newline at end of file
+}
