Exemplo n.º 1
0
    def get(self, mccm_id):
        """
        Cancel the MccM ticket provided in argument. Does not delete it but put the status as cancelled.
        """
        db = database('mccms')
        udb = database('users')

        mcm_mccm = mccm(db.get(mccm_id))
        curr_user = user(udb.get(mcm_mccm.current_user))

        self.logger.info("Canceling an mccm: %s" % (mccm_id))

        if mcm_mccm.get_attribute('status') == 'done':
            self.logger.info("You cannot cancel 'done' mccm ticket")
            return {"results": False, "message": "Cannot cancel done tickets"}

        if not mcm_mccm.get_attribute("pwg") in curr_user.get_pwgs():
            self.logger.info("User's PWGs: %s doesnt include ticket's PWG: %s" % (
                    curr_user.get_pwgs(), mcm_mccm.get_attribute("pwg")))

            return {"results": False, "message": "You cannot cancel ticket with different PWG than yours"}

        mcm_mccm.set_attribute('status', 'cancelled')
        mcm_mccm.update_history({'action': 'cancelled'})
        saved = db.update(mcm_mccm.json())
        if saved:
            return {"results": True}
        else:
            return {"results": False, "message": "Could not save the ticket to be cancelled."}
Exemplo n.º 2
0
    def get(self, mccm_id):
        """
        Cancel the MccM ticket provided in argument. Does not delete it but put the status as cancelled.
        """
        db = database('mccms')
        udb = database('users')

        mcm_mccm = mccm(db.get(mccm_id))
        curr_user = user(udb.get(mcm_mccm.current_user))

        self.logger.info("Canceling an mccm: %s" % (mccm_id))

        if mcm_mccm.get_attribute('status') == 'done':
            self.logger.info("You cannot cancel 'done' mccm ticket")
            return {"results": False, "message": "Cannot cancel done tickets"}

        if not mcm_mccm.get_attribute("pwg") in curr_user.get_pwgs():
            self.logger.info("User's PWGs: %s doesnt include ticket's PWG: %s" % (
                    curr_user.get_pwgs(), mcm_mccm.get_attribute("pwg")))

            return {"results": False, "message": "You cannot cancel ticket with different PWG than yours"}

        mcm_mccm.set_attribute('status', 'cancelled')
        mcm_mccm.update_history({'action': 'cancelled'})
        saved = db.update(mcm_mccm.json())
        if saved:
            return {"results": True}
        else:
            return {"results": False, "message": "Could not save the ticket to be cancelled."}
Exemplo n.º 3
0
    def GET(self, *args):
        """
        Goes through database and fills full names of all the users, who have not had it filled yet
        """
        db = database('users')
        users = db.get_all()
        results = []
        for u_d in users:
            u = user(u_d)
            if not u.get_attribute('fullname'):
                import subprocess
                import re

                output = subprocess.Popen([
                    "phonebook", "-t", "firstname", "-t", "surname", "--login",
                    u.get_attribute('username')
                ],
                                          stdout=subprocess.PIPE)
                split_out = [
                    x for x in re.split("[^a-zA-Z0-9_\-]",
                                        output.communicate()[0])
                    if x and x != "-"
                ]
                fullname = " ".join(split_out)
                u.set_attribute('fullname', fullname)
                results.append(
                    (u.get_attribute('username'), db.save(u.json())))
        return dumps({"results": results})
Exemplo n.º 4
0
 def change_role(self, username, action):
     db = database(self.db_name)
     doc = user(db.get(username))
     current_role = doc.get_attribute("role")
     if action == '-1':
         if current_role != self.all_roles[0]:
             doc.set_attribute(
                 "role",
                 self.all_roles[self.all_roles.index(current_role) - 1])
             authenticator.set_user_role(username,
                                         doc.get_attribute("role"))
             doc.update_history({
                 'action': 'decrease',
                 'step': doc.get_attribute("role")
             })
             return {"results": db.update(doc.json())}
         return {
             "results": username + " already is user"
         }  # else return that hes already a user
     if action == '1':
         if len(self.all_roles) != self.all_roles.index(
                 current_role) + 1:  # if current role is not the top one
             doc.set_attribute(
                 "role",
                 self.all_roles[self.all_roles.index(current_role) + 1])
             authenticator.set_user_role(username,
                                         doc.get_attribute("role"))
             doc.update_history({
                 'action': 'increase',
                 'step': doc.get_attribute("role")
             })
             return {"results": db.update(doc.json())}
         return {"results": username + " already has top role"}
     return {"results": "Failed to update user: "******" role"}
Exemplo n.º 5
0
 def change_role(self, username, action):
     db = database(self.db_name)
     doc = user(db.get(username))
     user_p = user_pack()
     current_user = user(db.get(user_p.get_username()))
     current_role = doc.get_attribute("role")
     if action == '-1':
         if current_role != 'user': #if not the lowest role -> then him lower himself
             doc.set_attribute("role", self.all_roles[self.all_roles.index(current_role) - 1])
             self.authenticator.set_user_role(username, doc.get_attribute("role"))
             return {"results": db.update(doc.json())}
         return {"results": username + " already is user"} #else return that hes already a user
     if action == '1':
         if current_user.get_attribute("role") != "administrator":
             return {"results": "Only administrators can upgrade roles"}
         if len(self.all_roles) != self.all_roles.index(current_role) + 1: #if current role is not the top one
             doc.set_attribute("role", self.all_roles[self.all_roles.index(current_role) + 1])
             self.authenticator.set_user_role(username, doc.get_attribute("role"))
             return {"results": db.update(doc.json())}
         return {"results": username + " already has top role"}
     return {"results": "Failed to update user: "******" role"}
Exemplo n.º 6
0
 def change_role(self, username, action):
     db = database(self.db_name)
     doc = user(db.get(username))
     user_p = user_pack()
     current_user = user(db.get(user_p.get_username()))
     current_role = doc.get_attribute("role")
     if action == '-1':
         if current_role != self.all_roles[0]:
             doc.set_attribute("role", self.all_roles[self.all_roles.index(current_role) - 1])
             self.authenticator.set_user_role(username, doc.get_attribute("role"))
             doc.update_history({'action': 'decrease' , 'step':doc.get_attribute("role")})
             return {"results": db.update(doc.json())}
         return {"results": username + " already is user"} #else return that hes already a user
     if action == '1':
         if len(self.all_roles) != self.all_roles.index(current_role) + 1: #if current role is not the top one
             doc.set_attribute("role", self.all_roles[self.all_roles.index(current_role) + 1])
             self.authenticator.set_user_role(username, doc.get_attribute("role"))
             doc.update_history({'action': 'increase' , 'step':doc.get_attribute("role")})
             return {"results": db.update(doc.json())}
         return {"results": username + " already has top role"}
     return {"results": "Failed to update user: "******" role"}
Exemplo n.º 7
0
 def PUT(self):
     """
     Save the information about a given user
     """
     db = database(self.db_name)
     data = threaded_loads(cherrypy.request.body.read().strip())
     new_user = user(data)
     if '_rev' in data:
         new_user.update_history({'action': 'updated'})
         return dumps({"results": db.update(new_user.json())})
     else:
         new_user.update_history({'action': 'created'})
         return dumps({"results": db.save(new_user.json())})
Exemplo n.º 8
0
 def PUT(self):
     """
     Save the information about a given user
     """
     db = database(self.db_name)
     data = threaded_loads(cherrypy.request.body.read().strip())
     new_user = user(data)
     if '_rev' in data:
         new_user.update_history({'action': 'updated'})
         return dumps({"results": db.update(new_user.json())})
     else:
         new_user.update_history({'action': 'created'})
         return dumps({"results": db.save(new_user.json())})
Exemplo n.º 9
0
    def put(self):
        """
        Save the information about a given user
        """

        db = database(self.db_name)
        data = loads(request.data.strip())
        new_user = user(data)
        self.logger.debug("is trying to update entry for %s" %
                          (new_user.get_attribute('username')))
        if '_rev' in data:
            new_user.update_history({'action': 'updated'})
            return {"results": db.update(new_user.json())}
        else:
            new_user.update_history({'action': 'created'})
            return {"results": db.save(new_user.json())}
Exemplo n.º 10
0
    def add_user(self):
        db = database(self.db_name)
        user_p = user_pack()
        if db.document_exists(user_p.get_username()):
            return {"results": "User {0} already in database".format(user_p.get_username())}
        mcm_user = user({"_id": user_p.get_username(),
                         "username": user_p.get_username(),
                         "email": user_p.get_email(),
                         "role": roles[access_rights.user],
                         "fullname": user_p.get_fullname()})

        # save to db
        if not db.save(mcm_user.json()):
            self.logger.error('Could not save object to database')
            return {"results": False}
        return {"results": True}
Exemplo n.º 11
0
    def get(self, user_id=None):
        """
        Retrieve the pwg of the provided user
        """
        # this could be a specific database in couch, to hold the list, with maybe some added information about whatever the group does...

        all_pwgs = settings.get_value('pwg')
        db = database('users')

        all_pwgs.sort()
        if user_id is None:
            return {"results": all_pwgs}
        if db.document_exists(user_id):
            mcm_user = user(db.get(user_id))
            return {"results": mcm_user.get_pwgs()}
        else:
            return {"results": []}
Exemplo n.º 12
0
    def get(self, pwgs):
        """
        Ask for the increase of the role of the current user to the given pwg
        """
        # get who's there
        user_p = user_pack()
        udb = database(self.db_name)
        mcm_u = user(udb.get(user_p.get_username()))
        # get the requested pwgs
        pwgs = pwgs.split(',')
        # set the pwgs to the current user
        current = mcm_u.get_attribute('pwg')
        current = list(set(current + pwgs))
        mcm_u.set_attribute('pwg', current)
        mcm_u.update_history({'action': 'ask role', 'step': pwgs})
        udb.update(mcm_u.json())

        # get the production managers emails
        __query = udb.construct_lucene_query({'role': 'production_manager'})
        production_managers = udb.full_text_search('search', __query, page=-1)
        # send a notification to prod manager + service
        to_who = map(lambda u: u['email'], production_managers) + [
            settings.get_value('service_account')
        ]
        to_who.append(user_p.get_email())
        com = communicator()
        l_type = locator()
        subject = 'Increase role for user %s' % mcm_u.get_attribute('fullname')
        message = 'Please increase the role of the user %s to the next level.\n\n%susers?prepid=%s' % (
            mcm_u.get_attribute('username'), l_type.baseurl(),
            mcm_u.get_attribute('username'))
        notification(subject,
                     message, [],
                     group=notification.USERS,
                     action_objects=[mcm_u.get_attribute('prepid')],
                     object_type='users',
                     target_role='production_manager')
        com.sendMail(to_who, subject, message)

        return {
            "results":
            True,
            "message":
            "user %s in for %s" % (mcm_u.get_attribute('username'), current)
        }
Exemplo n.º 13
0
    def GET(self, *args):
        """
        Ask for the increase of the role of the current user to the given pwg
        """
        if not args:
            return dumps({"results": False, "Message": "not pwg provided"})

        ## get who's there
        user_p = user_pack()
        udb = database(self.db_name)
        mcm_u = user(udb.get(user_p.get_username()))

        ## get the requested pwgs
        pwgs = args[0].split(',')
        #### set the pwgs to the current user
        current = mcm_u.get_attribute('pwg')
        current = list(set(current + pwgs))
        mcm_u.set_attribute('pwg', current)
        mcm_u.update_history({'action': 'ask role', 'step': args[0]})
        udb.update(mcm_u.json())

        ## get the production managers emails
        production_managers = udb.queries(['role==production_manager'])
        ### send a notification to prod manager + service
        to_who = map(lambda u: u['email'], production_managers) + [
            settings().get_value('service_account')
        ]
        to_who.append(user_p.get_email())
        com = communicator()
        l_type = locator()
        com.sendMail(
            to_who,
            'Increase role for user %s' % mcm_u.get_attribute('fullname'),
            'Please increase the role of the user %s to the next level.\n\n%susers?prepid=%s'
            % (mcm_u.get_attribute('username'), l_type.baseurl(),
               mcm_u.get_attribute('username')))

        return dumps({
            "results":
            True,
            "message":
            "user %s in for %s" % (mcm_u.get_attribute('username'), current)
        })
Exemplo n.º 14
0
    def GET(self, *args):
        """
        Retrieve the pwg of the provided user
        """
        ## this could be a specific database in couch, to hold the list, with maybe some added information about whatever the group does...

        all_pwgs = settings().get_value('pwg')
        db = database(self.db_name)

        all_pwgs.sort()
        if len(args) == 0:
            return dumps({"results": all_pwgs})
        user_name = args[0]
        if db.document_exists(user_name):
            mcm_user = user(db.get(args[0]))
            if mcm_user.get_attribute('role') in ['production_manager', 'administrator', 'generator_convener']:
                return dumps({"results": all_pwgs})
            else:
                return dumps({"results": mcm_user.get_attribute('pwg')})
        else:
            return dumps({"results": []})
Exemplo n.º 15
0
    def GET(self, *args):
        """
        Goes through database and fills full names of all the users, who have not had it filled yet
        """
        db = database('users')
        users = db.get_all()
        results = []
        for u_d in users:
            u = user(u_d)
            if not u.get_attribute('fullname'):
                import subprocess
                import re

                output = subprocess.Popen(
                    ["phonebook", "-t", "firstname", "-t", "surname", "--login", u.get_attribute('username')],
                    stdout=subprocess.PIPE)
                split_out = [x for x in re.split("[^a-zA-Z0-9_\-]", output.communicate()[0]) if x and x != "-"]
                fullname = " ".join(split_out)
                u.set_attribute('fullname', fullname)
                results.append((u.get_attribute('username'), db.save(u.json())))
        return dumps({"results": results})
Exemplo n.º 16
0
    def GET(self, *args):
        """
        Ask for the increase of the role of the current user to the given pwg
        """
        if not args:
            return dumps({"results" : False, "Message" : "not pwg provided"})

        ## get who's there
        user_p = user_pack()
        udb = database(self.db_name)
        mcm_u = user( udb.get( user_p.get_username()))

        ## get the requested pwgs
        pwgs = args[0].split(',')
        #### set the pwgs to the current user
        current = mcm_u.get_attribute('pwg')
        current = list(set(current+pwgs))
        mcm_u.set_attribute('pwg', current)
        mcm_u.update_history({'action':'ask role','step' : args[0]})
        udb.update(mcm_u.json())

        ## get the production managers emails
        production_managers = udb.queries(['role==production_manager'])
        ### send a notification to prod manager + service
        to_who = map(lambda u: u['email'], production_managers) + [settings().get_value('service_account')]
        to_who.append( user_p.get_email() )
        com = communicator()
        l_type = locator()
        com.sendMail( to_who,
                      'Increase role for user %s' % mcm_u.get_attribute('fullname'),
                      'Please increase the role of the user %s to the next level.\n\n%susers?prepid=%s' % ( mcm_u.get_attribute('username'),
                                                                                                            l_type.baseurl(),
                                                                                                            mcm_u.get_attribute('username')
                                                                                                            ))

        

        return dumps({"results" : True, "message" : "user %s in for %s" %( mcm_u.get_attribute('username'), current)})
Exemplo n.º 17
0
    def GET(self, *args):
        """
        Retrieve the pwg of the provided user
        """
        ## this could be a specific database in couch, to hold the list, with maybe some added information about whatever the group does...

        all_pwgs = settings().get_value('pwg')
        db = database(self.db_name)

        all_pwgs.sort()
        if len(args) == 0:
            return dumps({"results": all_pwgs})
        user_name = args[0]
        if db.document_exists(user_name):
            mcm_user = user(db.get(args[0]))
            if mcm_user.get_attribute('role') in [
                    'production_manager', 'administrator', 'generator_convener'
            ]:
                return dumps({"results": all_pwgs})
            else:
                return dumps({"results": mcm_user.get_attribute('pwg')})
        else:
            return dumps({"results": []})
Exemplo n.º 18
0
    def add_user(self):
        db = database(self.db_name)
        user_p = user_pack()
        if db.document_exists(user_p.get_username()):
            return {
                "results":
                "User {0} already in database".format(user_p.get_username())
            }
        mcm_user = user({
            "_id": user_p.get_username(),
            "username": user_p.get_username(),
            "email": user_p.get_email(),
            "role": roles[access_rights.user],
            "fullname": user_p.get_fullname()
        })

        # save to db
        if not mcm_user.reload():
            self.logger.error('Could not save object to database')
            return {"results": False}

        mcm_user.update_history({'action': 'created'})
        mcm_user.reload()
        return {"results": True}