#!/usr/bin/env python
# 
# Copyright 2012 Google Inc. All Rights Reserved.
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# #

"""ACL Groups admin handler."""





import re
from simian.mac import admin
from simian.mac import models
from simian.mac.common import auth
from simian.mac.common import util

MAIL_REGEX = (
  '^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$')

class ACLGroups(admin.AdminHandler):

  def get(self, group=None):
    """GET handler."""
    if not self.IsAdminUser():
      self.error(403)
      return

    if group:
      group_members = self.GetMembers(group)
      d = {'report_type': 'acl_groups', 'list': [(gm,) for gm in group_members],
           'columns': 1, 'regex': ['/%s/' % MAIL_REGEX],
           'title': 'ACL Group: %s' % group, 'back': '/admin/acl_groups',
           'infopanel': 'Full email address required (e.g. user@example.com)'}
      self.Render('list_edit.html', d)
    else:
      group_data = []
      for name, title in auth.ACL_GROUPS.items():
        members = self.GetMembers(name)
        group_data.append({'name': name, 'title': title, 'members': members})
      d = {'report_type': 'acl_groups', 'groups': group_data}
      self.Render('acl_groups.html', d)

  def post(self, group=None):
    """POST handler."""
    if not self.IsAdminUser():
      self.error(403)
      return

    if group:
      values = self.request.get_all('item_0', None)
      members = []
      is_email = re.compile(MAIL_REGEX)
      for member in values:
        if is_email.match(member):
          members.append(member)
        else:
          self.error(400)
          self.response.out.write('malformed email')
          return
      models.KeyValueCache.MemcacheWrappedSet(group, 'text_value',
                                              util.Serialize(members))
      self.redirect('/admin/acl_groups?msg=Group%20saved')

  def GetMembers(self, group_name):
    """Get a list of members belonging to the group"""
    members = models.KeyValueCache.MemcacheWrappedGet(group_name, 'text_value')
    member_list = []
    if members:
      try:
        member_list = util.Deserialize(members)
      except util.DeserializeError:
        pass
    return member_list
@@ -816,71 +816,50 @@
 X = 
-(%0A  '%5E%5B_a-z0-9-%5D+(%5C.%5B_a-z0-9-%5D+)*@%5B
+r'%5Cb%5BA-Za-z0-9._%25+-%5D+@%5BA-Z
 a-z0-9
+.
 -%5D+
-(
 %5C.%5B
-a-z0-9-%5D+)*(%5C.%5B
+A-Z
 a-z%5D
@@ -867,12 +867,11 @@
 2,4%7D
-)$')
+%5Cb'
 %0A%0Acl
@@ -2693,20 +2693,21 @@
   return member_list
+%0A

Update MAIL_REGEX in acl_groups.py, fixing Admin UI JS validation