Example #1
0
    def post(self):
        username = self.get_argument("username", "").strip()
        password = self.get_argument("password", "").strip()
        redirect = self.get_argument("next", "/").strip()

        errors = []
        if not username:
            errors.append("Username empty.")
        if not password:
            errors.append("Password empty.")

        user = None
        try:
            user = User.get(User.username == username)
            if not user.password == hashlib.md5(
                    password.encode("utf-8")).hexdigest():
                errors.append("Wrong password.")
        except User.DoesNotExist as e:
            errors.append("Wrong username.")

        if errors:
            return self.render("login.htm",
                               next=self.get_argument("next", "/"),
                               errors=errors)

        # OK, everything is fine.
        self.set_current_user(user)
        return self.redirect(redirect)
Example #2
0
    def get(self, username, reponame):
        user = User.get_or_none(User.username == username)
        if not user:
            return self.info("user not exists")

        repo = Repo.get_or_none(Repo.reponame == reponame)
        if not repo:
            return self.info("repo not exists")

        if username != self.current_user.username:
            return self.info("not authorized")

        repopath = os.path.join(REPO_ROOT, user.username, repo.reponame)

        try:
            Repo.delete().where(Repo.rid == repo.rid).execute()
        except Exception as e:
            return self.info("failed to delete repo from database!" + str(e))

        try:
            if os.path.exists(repopath):
                shutil.rmtree(repopath, ignore_errors=False, onerror=None)
        except OSError as e:
            return self.info("failed to delete repo from filesystem!" + str(e))

        return self.redirect("/")
Example #3
0
    def get(self, username=None):
        user = None
        if username:
            try:
                user = User.get(User.username == username)
            except User.DoesNotExist as e:
                return self.info("User does not exist!")
        else:
            user = self.current_user
            if user: self.scan(user)

        if not user:
            return self.redirect("/!welcome")

        page = int(self.get_argument("p", 1))
        total = user.repos.count()
        paginator = PageInfo(url="/?p=", total=total, page=page)

        if user != self.current_user:
            repos = Repo.select().where(
                (Repo.public == True) & (Repo.user_id == user.uid)).paginate(
                    paginator.page_now, paginator.unit).order_by(-Repo.ctime)
        else:
            repos = user.repos.paginate(paginator.page_now,
                                        paginator.unit).order_by(-Repo.ctime)

        for repo in repos:
            try:
                repo.git = pygit2.Repository(
                    os.path.join(REPO_ROOT, user.username, repo.reponame))

                if tornado.options.options.perf:
                    repo.total_commit = -1
                elif repo.git.is_empty:
                    repo.total_commit = 0
                else:
                    cmd = GitCommand("git rev-list")
                    cmd.addParam("--count")
                    cmd.addParam("--all")
                    cmd.setDir(
                        os.path.join(REPO_ROOT, user.username, repo.reponame))
                    result = cmd.run(self.request.body)
                    repo.total_commit = int(result.decode("utf-8"))

            except pygit2.GitError as e:
                print(e)
                repo.git = None
            except GitCommandException as e:
                print(repo.reponame, e)
                repo.total_commit = "unknown"
            except UnicodeDecodeError as e:
                print(e)
                repo.total_commit = "unknown"
            except Exception as e:
                repo.total_commit = "unknown"
                print(repo.reponame, e)
        self.render("userhome.htm",
                    user=user,
                    repos=repos,
                    paginator=paginator)
Example #4
0
 def get(self, action):
     if action == "edituser":
         username = self.get_argument("username", None)
         uid = self.get_argument("uid", None)
         user = User.get_or_none(User.uid == uid) or self.current_user
         if not user:
             return self.finish("false")
         elif user.username == username and user.uid != uid:
             return self.finish("false")
         return self.finish("true")
     elif action == "newuser":
         username = self.get_argument("username", None)
         email = self.get_argument("email", None)
         user = User.get_or_none(
             User.username == username) or self.current_user
         if user:
             return self.finish("false")
         return self.finish("true")
     elif action == "newrepo":  # true means OK
         reponame = self.get_argument("reponame", None)
         username = self.get_argument("username", None)
         user = User.get_or_none(
             User.username == username) or self.current_user
         if not user:
             return self.finish("false")
         for repo in user.repos:
             if repo.reponame == reponame:
                 return self.finish("false")
         return self.finish("true")
     elif action == "editrepo":  # true means OK
         rid = self.get_argument("rid", None)
         reponame = self.get_argument("reponame", None)
         username = self.get_argument("username", None)
         user = User.get_or_none(User.username == username)
         if not user:
             return self.finish("false")
         for repo in user.repos:
             if repo.reponame == reponame:
                 return self.finish("true" if repo.rid == rid else "false")
         return self.finish("true")
     else:
         return self.finish("false")
Example #5
0
    def get(self, username=None):
        if username:
            if username != self.current_user.username and not self.current_user.admin(
            ):
                return self.info("not authorized")
            user = User.get(User.username == username)
        else:
            user = self.current_user

        # user = DATABASE.getAdministrator()
        return self.render("useredit.htm", user=user, errors=[])
Example #6
0
    def get(self, username=None):

        if not username:
            user = self.current_user
            if not user:
                return self.redirect("/")
        else:
            try:
                user = User.get(User.username == username)
            except User.DoesNotExist as e:
                return self.info("User Not Found!")

        user.intro = safe_markdown(user.intro)
        return self.render("userinfo.htm", user=user)
Example #7
0
    def post(self, username=None):
        username = self.get_argument("username").strip()
        nickname = self.get_argument("nickname").strip()
        password = self.get_argument("password").strip()
        password2 = self.get_argument("password2").strip()
        email = self.get_argument("email").strip()
        usertype = self.get_argument("usertype", "administrator").strip()
        intro = self.get_argument("intro").strip()
        uid = self.get_argument("uid").strip()
        errors = []

        # Check user input
        if len(username) < 2 or len(username) > 32:
            errors.append("Username length should be between 2-32 characters.")
        if not re.match(r"[_\w]+", username):
            errors.append(
                "Username should consist of numbers, letters and underscore.")
        if password and password != password2:
            errors.append("Password mismatch.")
        if email and not re.match(r"[\.\w]+@[\.\w]+\w", email):
            errors.append("Invalid Email address.")

        user = User.get_or_none(User.uid == uid)
        if not user:
            errors.append("Wrong username.")

        if errors:
            return self.render("useredit.htm", user=user, errors=errors)

        if user.username != username:
            try:
                os.rename(os.path.join(REPO_ROOT, user.username),
                          os.path.join(REPO_ROOT, username))
            except OSError as e:
                return self.info("failed to rename the repo. " + str(e))

        # OK, everything is fine.
        user.username = username
        user.nickname = nickname
        user.email = email
        user.intro = intro
        if password:
            user.password = hashlib.md5(password.encode("utf-8")).hexdigest()

        user.save()
        self.set_current_user(user)
        self.redirect("/{}/!info".format(username))
Example #8
0
    def user_repo_access(self, username, reponame):
        user = User.get_or_none(User.username == username)
        repo = Repo.get_or_none(Repo.reponame == reponame)

        if not user:
            return None, None, "user not available"

        if not repo:
            return None, None, "repo not available"

        if not repo.public:
            if self.current_user != user:
                return None, None, "not authorized"
            if self.current_user.uid != repo.owner.uid:
                return None, None, "not authorized"

        return user, repo, None
Example #9
0
 def prepare(self):
     if self.request.uri != "/!init" and User.select().count() == 0:
         return self.redirect("/!init")
     return super().prepare()
Example #10
0
 def get_current_user(self):
     username = self.get_secure_cookie("username")
     try:
         return User.get(User.username == username)
     except User.DoesNotExist as e:
         return None
Example #11
0
    def get(self):

        if User.select().count() == 0:
            return self.add_first_user()
        else:
            return self.add_user()
Example #12
0
 def get(self):
     users = User.select()
     return self.render("userlist.htm", users=users)
Example #13
0
    def post(self):

        username = self.get_argument("username").strip()
        nickname = self.get_argument("nickname", username).strip()
        password = self.get_argument("password").strip()
        password2 = self.get_argument("password2").strip()
        email = self.get_argument("email").strip()
        usertype = self.get_argument("usertype", "administrator").strip()
        intro = self.get_argument("intro").strip()

        errors = []

        # Check user input
        if len(username) < 2 or len(username) > 32:
            errors.append("Username length should be between 2-32 characters.")
        if not re.match(r"[_\w]+", username) or len(username) < 2:
            errors.append(
                "Username should consist of numbers, letters and underscore.")
        if not password:
            errors.append("Password empty.")
        elif password != password2:
            errors.append("Password mismatch.")
        if email and not re.match(r"[\.\w]+@[\.\w]+\w", email):
            errors.append("Invalid Email address.")

        if errors:
            return self.render("useredit.htm", errors=errors)

        password = hashlib.md5(password.encode("utf-8")).hexdigest()

        user = User()
        user.uid = uuid.uuid4()
        user.username = username
        user.nickname = nickname
        user.password = password
        user.intro = intro
        user.email = email
        user.locked = True
        if User.select().count() == 0:
            user.role = 99
        else:
            user.role = 10
        user.save(force_insert=True)

        self.redirect("/")