예제 #1
0
def add_co_person_roles(co_person: dict) -> None:
    co_person_id = co_person.get('Id')
    co_roles = api.coperson_roles_view_per_coperson(
        coperson_id=co_person_id).get('CoPersonRoles', [])
    ns_person = NotaryServiceUser.objects.filter(
        co_person_id=co_person_id).first()
    if co_roles and ns_person:
        for co_role in co_roles:
            co_role_id = co_role.get('Id')
            ns_role = Role.objects.filter(co_role_id=co_role_id).first()
            if ns_role:
                logger.info('{0} - Found CoPersonRole: {1} - {2}'.format(
                    datetime.datetime.now().ctime(), ns_role.co_cou.name,
                    ns_role.co_role_id))
            else:
                co_cou_id = co_role.get('CouId', None)
                if co_cou_id:
                    ns_role = Role()
                    ns_role.co_role_id = co_role_id
                    ns_role.co_person_id = co_person_id
                    ns_role.co_cou = ComanageCou.objects.filter(
                        co_cou_id=co_cou_id).first()
                    ns_role.save()
                    ns_person.roles.add(ns_role)
                    ns_person.save()
                    logger.info('{0} - Add CoPersonRole: {1} - {2}'.format(
                        datetime.datetime.now().ctime(), ns_role.co_cou.name,
                        ns_role.co_role_id))
                else:
                    logger.warning(
                        '{0} - Missing CouId: CoPersonRole - {1}'.format(
                            datetime.datetime.now().ctime(), co_role_id))
예제 #2
0
def edit(rec_id=None):
    setExits()
    g.title = "Edit {} Record".format(g.title)

    role = Role(g.db)
    rec = None

    if rec_id == None:
        rec_id = request.form.get('id', request.args.get('id', -1))

    rec_id = cleanRecordID(rec_id)
    #import pdb;pdb.set_trace

    if rec_id < 0:
        flash("That is not a valid ID")
        return redirect(g.listURL)

    if not request.form:
        """ if no form object, send the form page """
        if rec_id == 0:
            rec = role.new()
        else:
            rec = role.get(rec_id)
            if not rec:
                flash("Unable to locate that record")
                return redirect(g.listURL)
    else:
        #have the request form
        #import pdb;pdb.set_trace()
        if rec_id and request.form['id'] != 'None':
            rec = role.get(rec_id)
        else:
            # its a new unsaved record
            rec = role.new()
            role.update(rec, request.form)

        if validForm(rec):
            #update the record
            role.update(rec, request.form)
            # make names lower case
            rec.name = request.form['name'].lower().strip()

            try:
                role.save(rec)
                g.db.commit()
            except Exception as e:
                g.db.rollback()
                flash(
                    printException(
                        'Error attempting to save ' + g.title + ' record.',
                        "error", e))

            return redirect(g.listURL)

        else:
            # form did not validate
            pass

    # display form
    return render_template('role/role_edit.html', rec=rec)
예제 #3
0
def create_co_person_role(co_person_id: int, co_cou_id: int) -> bool:
    response = False
    new_co_person_role = api.coperson_roles_add(coperson_id=co_person_id,
                                                cou_id=co_cou_id)
    if new_co_person_role:
        co_role_id = new_co_person_role.get('Id')
        ns_person = NotaryServiceUser.objects.filter(
            co_person_id=co_person_id).first()
        ns_role = Role()
        ns_role.co_role_id = co_role_id
        ns_role.co_person_id = co_person_id
        ns_role.co_cou = ComanageCou.objects.filter(
            co_cou_id=co_cou_id).first()
        ns_role.save()
        ns_person.roles.add(ns_role)
        ns_person.save()
        logger.info('{0} - Add CoPersonRole: {1} - {2}'.format(
            datetime.datetime.now().ctime(), ns_role.co_cou.name,
            ns_role.co_role_id))
        response = True
    else:
        logger.info(
            '{0} - Failed to add CoPersonRole - CoPersonId: {1}'.format(
                datetime.datetime.now().ctime(), co_person_id))

    return response
예제 #4
0
async def create_role(request: Request) -> Dict:
    """
    创建角色
    """
    request_body = request.json
    name = request_body["name"]
    description = request_body["description"]
    role = Role(name=name, description=description)
    role.save()
    return {"message": "创建成功"}
예제 #5
0
    def handle(self, *args, **options):
        group = Group()
        group.name = 'Trainee'
        group.save()

        role = Role()
        role.code = 'trainee'
        role.name = 'Trainee'
        role.save()

        role = Roles()
        role.name = 'trainee'
        role.display_name = 'Trainee'
        role.save()
예제 #6
0
def roles(request):
    if request.POST:
        print request.POST
        from django.core.validators import validate_email
        from django.core.exceptions import ValidationError

        try:
            validate_email(request.POST['user'])
            try:
                user = User.objects.get(email=request.POST['user'])
                group = Group.objects.get(name=request.POST['group'])
                try:
                    Role.objects.get(user=user, company=request.company, group=group)
                    messages.error(request,
                                   'User ' + user.username + ' (' + user.email + ') is already the ' + request.POST[
                                       'group'] + '.')
                except Role.DoesNotExist:
                    role = Role(user=user, company=request.company, group=group)
                    role.save()
                    messages.success(request,
                                     'User ' + user.username + ' (' + user.email + ') added as ' + request.POST[
                                         'group'] + '.')
            except User.DoesNotExist:
                messages.error(request, 'No users found with the e-mail address ' + request.POST['user'])
        except ValidationError:
            try:
                user = User.objects.get(username=request.POST['user'])
                group = Group.objects.get(name=request.POST['group'])
                try:
                    Role.objects.get(user=user, company=request.company, group=group)
                    messages.error(request,
                                   'User ' + user.username + ' (' + user.email + ') is already the ' + request.POST[
                                       'group'] + '.')
                except Role.DoesNotExist:
                    role = Role(user=user, company=request.company, group=group)
                    role.save()
                    messages.success(request,
                                     'User ' + user.username + ' (' + user.email + ') added as ' + request.POST[
                                         'group'] + '.')
            except User.DoesNotExist:
                messages.error(request, 'No users found with the username ' + request.POST['user'])
    objs = Role.objects.filter(company=request.company)
    return render(request, 'roles.html', {'roles': objs})
예제 #7
0
    def create(self, request):
        data = request.data.copy()
        permissions = data.pop('permissions', [])

        if not self.request.user.has_perm('users.add_role'):
            return Response("User has no permissions to create a Role",
                            status=status.HTTP_403_FORBIDDEN)

        all_permissions_group = PermissionConfig.objects.first()
        try:
            with transaction.atomic():
                group = Group(name='{}-{}'.format(
                    data['name'], request.user.aerosimple_user.airport))

                group.save()

                for perm in permissions:
                    p = Permission.objects.get(id=perm)

                    if p in all_permissions_group.permissions.all():
                        group.permissions.add(p)
                    else:
                        group.delete()
                        return Response(
                            "id {} is not a valid Permission".format(p.id),
                            status=status.HTTP_400_BAD_REQUEST)

                role = Role(name=data['name'])
                role.permission_group = group
                role.airport = request.user.aerosimple_user.airport
                role.save()

        except ValidationError:
            raise

        return Response(RoleSerializer(role).data,
                        status=status.HTTP_201_CREATED)
예제 #8
0
 def _bootstrap_role(self, role):
     role = Role(role=role)
     role.save()
     return role