app/soc/logic/accounts.py
author Mario Ferraro <fadinlight@gmail.com>
Sun, 15 Nov 2009 22:12:20 +0100
changeset 3093 d1be59b6b627
parent 2345 f78caf12f32d
permissions -rw-r--r--
GMaps related JS changed to use new google namespace. Google is going to change permanently in the future the way to load its services, so better stay safe. Also this commit shows uses of the new melange.js module. Fixes Issue 634.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     2
#
1308
35b75ffcbb37 Partially reverted "Update the copyright notice for 2009."
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1307
diff changeset
     3
# Copyright 2008 the Melange authors.
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     4
#
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     8
#
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    10
#
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    15
# limitations under the License.
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    16
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 457
diff changeset
    17
"""Basic Google Account and User (Model) query functions.
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    18
"""
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    19
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    20
__authors__ = [
229
a46c238be8db Show link to /site/user/list on /site/user/lookup when User is not found.
Todd Larsen <tlarsen@google.com>
parents: 223
diff changeset
    21
  '"Chen Lunpeng" <forever.clp@gmail.com>',
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    22
  '"Todd Larsen" <tlarsen@google.com>',
1527
0071733da7cd Simplify the account retrieval logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1520
diff changeset
    23
  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    24
  ]
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    25
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    26
390
d12c95ade374 Remove unused imports, fix too long lines and indentions.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 355
diff changeset
    27
from google.appengine.api import users
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    28
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    29
1600
0aa3de1b2acc Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1547
diff changeset
    30
def getCurrentAccount(normalize=True):
0aa3de1b2acc Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1547
diff changeset
    31
  """Returns an optionally normalized version of the current account.
0aa3de1b2acc Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1547
diff changeset
    32
  """
0aa3de1b2acc Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1547
diff changeset
    33
0aa3de1b2acc Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1547
diff changeset
    34
  account = users.get_current_user()
0aa3de1b2acc Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1547
diff changeset
    35
  return normalizeAccount(account) if (account and normalize) else account
0aa3de1b2acc Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1547
diff changeset
    36
0aa3de1b2acc Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1547
diff changeset
    37
2345
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1619
diff changeset
    38
def getCurrentUserId():
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1619
diff changeset
    39
  """Returns a unique id of the current user.
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1619
diff changeset
    40
  """
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1619
diff changeset
    41
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1619
diff changeset
    42
  return users.get_current_user().user_id()
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1619
diff changeset
    43
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1619
diff changeset
    44
1520
ac407f5b10dc Add a normalizeAccount to logic.accounts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1519
diff changeset
    45
def normalizeAccount(account):
ac407f5b10dc Add a normalizeAccount to logic.accounts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1519
diff changeset
    46
  """Returns a normalized version of the specified account.
ac407f5b10dc Add a normalizeAccount to logic.accounts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1519
diff changeset
    47
  """
ac407f5b10dc Add a normalizeAccount to logic.accounts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1519
diff changeset
    48
ac407f5b10dc Add a normalizeAccount to logic.accounts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1519
diff changeset
    49
  normalized = str(account).lower()
ac407f5b10dc Add a normalizeAccount to logic.accounts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1519
diff changeset
    50
ac407f5b10dc Add a normalizeAccount to logic.accounts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1519
diff changeset
    51
  if account.email() == normalized:
ac407f5b10dc Add a normalizeAccount to logic.accounts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1519
diff changeset
    52
    return account
ac407f5b10dc Add a normalizeAccount to logic.accounts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1519
diff changeset
    53
ac407f5b10dc Add a normalizeAccount to logic.accounts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1519
diff changeset
    54
  return users.User(email=normalized)
ac407f5b10dc Add a normalizeAccount to logic.accounts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1519
diff changeset
    55
2345
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1619
diff changeset
    56
1547
c8c269ef0498 Added a denormalizeAccount
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1527
diff changeset
    57
def denormalizeAccount(account):
c8c269ef0498 Added a denormalizeAccount
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1527
diff changeset
    58
  """Returns a denormalized version of the specified account.
c8c269ef0498 Added a denormalizeAccount
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1527
diff changeset
    59
  """
c8c269ef0498 Added a denormalizeAccount
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1527
diff changeset
    60
c8c269ef0498 Added a denormalizeAccount
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1527
diff changeset
    61
  if account.email().find('@') != -1:
c8c269ef0498 Added a denormalizeAccount
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1527
diff changeset
    62
    return account
c8c269ef0498 Added a denormalizeAccount
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1527
diff changeset
    63
c8c269ef0498 Added a denormalizeAccount
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1527
diff changeset
    64
  domain = account.auth_domain()
c8c269ef0498 Added a denormalizeAccount
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1527
diff changeset
    65
  denormalized = ''.join([account.email(), '@', domain])
c8c269ef0498 Added a denormalizeAccount
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1527
diff changeset
    66
c8c269ef0498 Added a denormalizeAccount
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1527
diff changeset
    67
  return users.User(email=denormalized)
c8c269ef0498 Added a denormalizeAccount
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1527
diff changeset
    68
2345
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1619
diff changeset
    69
1527
0071733da7cd Simplify the account retrieval logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1520
diff changeset
    70
def isDeveloper(account=None):
164
afdf502c6cc4 Documentation updates and one typo fix.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 141
diff changeset
    71
  """Returns True if a Google Account is a Developer with special privileges.
1527
0071733da7cd Simplify the account retrieval logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1520
diff changeset
    72
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 457
diff changeset
    73
  Since it only works on the current logged-in user, if account matches the
138
e1167bdf71a4 Improve the __doc__ string of isIdDeveloper() to explain better how it now
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
    74
  current logged-in Google Account, the App Engine Users API function
e1167bdf71a4 Improve the __doc__ string of isIdDeveloper() to explain better how it now
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
    75
  user.is_current_user_admin() is checked.  If that returns False, or
1527
0071733da7cd Simplify the account retrieval logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1520
diff changeset
    76
  account is not the currently logged-in user, False is returned.
0071733da7cd Simplify the account retrieval logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1520
diff changeset
    77
138
e1167bdf71a4 Improve the __doc__ string of isIdDeveloper() to explain better how it now
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
    78
  This solves the "chicken-and-egg" problem of no User entity having its
e1167bdf71a4 Improve the __doc__ string of isIdDeveloper() to explain better how it now
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
    79
  is_developer property set, but no one being able to set it.
1527
0071733da7cd Simplify the account retrieval logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1520
diff changeset
    80
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    81
  Args:
1527
0071733da7cd Simplify the account retrieval logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1520
diff changeset
    82
    account: a Google Account (users.User) object;
0071733da7cd Simplify the account retrieval logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1520
diff changeset
    83
      if not supplied, the current logged-in user is checked
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    84
  """
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
    85
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
    86
  # Get the currently logged in user
1600
0aa3de1b2acc Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1547
diff changeset
    87
  current = getCurrentAccount()
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
    88
1527
0071733da7cd Simplify the account retrieval logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1520
diff changeset
    89
  if current and (not account):
0071733da7cd Simplify the account retrieval logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1520
diff changeset
    90
    # default to the current user
0071733da7cd Simplify the account retrieval logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1520
diff changeset
    91
    account = current
0071733da7cd Simplify the account retrieval logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1520
diff changeset
    92
0071733da7cd Simplify the account retrieval logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1520
diff changeset
    93
  if not account:
137
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
    94
    # no Google Account was supplied or is logged in, so an unspecified
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
    95
    # User is definitely *not* a Developer
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
    96
    return False
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    97
1527
0071733da7cd Simplify the account retrieval logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1520
diff changeset
    98
  if (account == current) and users.is_current_user_admin():
0071733da7cd Simplify the account retrieval logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1520
diff changeset
    99
    # the current account should be checked, and it is in the
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
   100
    # Administration->Developers list in the App Engine console
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
   101
    return True
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
   102
1527
0071733da7cd Simplify the account retrieval logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1520
diff changeset
   103
  # account is not current user, or current user is not an admin
0071733da7cd Simplify the account retrieval logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1520
diff changeset
   104
  return False