'gives user_id membership of group_id or role
if user is None than user_id is that of current logged in user
S3: extended to support Entities'

def add_membership(self, group_id=None, user_id=None, role=None, entity=None):

    group_id = (group_id or self.id_group(role))
    try:
        group_id = int(group_id)
    except:
        group_id = self.id_group(group_id)
    if ((not user_id) and self.user):
        user_id = self.user.id
    membership = self.settings.table_membership
    record = membership(user_id=user_id, group_id=group_id, pe_id=entity)
    if record:
        return record.id
    else:
        membership_id = membership.insert(group_id=group_id, user_id=user_id, pe_id=entity)
    self.update_groups()
    self.log_event(self.messages.add_membership_log, dict(user_id=user_id, group_id=group_id))
    return membership_id
