Esempio n. 1
0
    def modify(self):
        self._object = user = usergroup.User(env.request.parameters.get("name", env.request.user.name))

        if not env.request.parameters["language"] in localization.getLanguagesIdentifiers():
            raise error.PrewikkaUserError(N_("Invalid Language"), N_("Specified language does not exist"), log_priority=log.WARNING)

        list(hookmanager.trigger("HOOK_USERMANAGEMENT_USER_MODIFY", user))
        if not env.request.parameters["timezone"] in localization.get_timezones():
            raise error.PrewikkaUserError(N_("Invalid Timezone"), N_("Specified timezone does not exist"), log_priority=log.WARNING)

        need_reload = False
        user.begin_properties_change()

        for param, reload in (("fullname", False), ("email", False), ("theme", True), ("language", True), ("timezone", False)):
            if user == env.request.user and reload and env.request.parameters.get(param) != user.get_property(param):
                need_reload = True

            user.set_property(param, env.request.parameters.get(param))

        if user == env.request.user:
            user.set_locale()

        user.commit_properties_change()

        # Make sure nothing is returned (reset the default dataset)
        env.request.dataset = None

        if need_reload:
            return response.PrewikkaDirectResponse({"type": "reload"})

        return response.PrewikkaRedirectResponse(url_for(".display"), 303)
Esempio n. 2
0
    def render(self):
        login = self.parameters.get("name", env.request.user.name)

        self._object = user = usergroup.User(login)

        env.db.set_property(user, "fullname", self.parameters.get("fullname"))
        env.db.set_property(user, "email", self.parameters.get("email"))
        env.db.set_property(user, "theme", self.parameters.get("theme"))

        lang = self.parameters["language"]
        if not lang in localization.getLanguagesIdentifiers():
            raise error.PrewikkaUserError(
                _("Invalid Language"), N_("Specified language does not exist"), log_priority=log.WARNING
            )

        env.db.set_property(user, "language", lang)
        if user == env.request.user:
            env.request.user.set_locale()

        timezone = self.parameters["timezone"]
        if not timezone in localization.get_timezones():
            raise error.PrewikkaUserError(
                _("Invalid Timezone"), N_("Specified timezone does not exist"), log_priority=log.WARNING
            )

        env.db.set_property(user, "timezone", timezone)

        # Make sure nothing is returned (reset the default dataset)
        self.dataset = None
def test_save():
    """
    Test `prewikka.views.usermanagement.save` view.
    """
    view = load_view_for_fixtures("usersettings.save")
    backup_parameters = deepcopy(env.request.parameters)

    # valid
    params = {
        'language': next(iter(localization.get_languages().keys())),
        'timezone': localization.get_timezones()[0],
    }
    env.request.parameters = params

    view.render(name=env.request.user.name)

    # FIXME
    # valid with new email
    # params_email = deepcopy(params)
    # params_email['email'] = '*****@*****.**'
    # env.request.parameters = params_email

    # view.render()

    # valid with new theme (reload page)
    params_email = deepcopy(params)
    params_email['theme'] = 'dark'
    env.request.parameters = params_email

    view.render(name=env.request.user.name)

    # FIXME
    # valid with different user
    # params_user = deepcopy(params)
    # params_user['name'] = 'test_different'
    # env.request.parameters = params_user

    # view.modify()

    # invalid language
    with pytest.raises(PrewikkaUserError):
        params_invalid = deepcopy(params)
        params_invalid['language'] = None
        env.request.parameters = params_invalid

        view.render(name=env.request.user.name)

    # invalid timezone
    with pytest.raises(PrewikkaUserError):
        params_invalid = deepcopy(params)
        params_invalid['timezone'] = None
        env.request.parameters = params_invalid

        view.render(name=env.request.user.name)

    # clean
    env.request.parameters = backup_parameters
Esempio n. 4
0
    def display(self):
        self._object = env.request.user

        if not env.auth.hasUser(self._object):
            raise error.PrewikkaUserError(N_("Invalid User"), N_("Requested user '%s' does not exist", self._object))

        dataset = {}
        dataset["object"] = self._object
        dataset["fullname"] = self._object.get_property("fullname")
        dataset["email"] = self._object.get_property("email")
        dataset["available_timezones"] = localization.get_timezones()
        dataset["timezone"] = self._object.get_property("timezone", default=env.config.general.default_timezone)
        dataset["available_languages"] = localization.getLanguagesAndIdentifiers()
        dataset["language"] = self._object.get_property("language", default=env.config.general.default_locale)
        dataset["available_themes"] = theme.getThemes()
        dataset["selected_theme"] = self._object.get_property("theme", default=env.config.general.default_theme)

        return template.PrewikkaTemplate(__name__, 'templates/usersettings.mak').render(**dataset)
Esempio n. 5
0
    def render(self):
        login = self.parameters.get("name")
        self._object = usergroup.User(login) if login else env.request.user

        if not env.auth.hasUser(self._object):
            raise error.PrewikkaUserError(_("Invalid User"), N_("Requested user '%s' does not exist", self._object))

        self.dataset["object"] = self._object
        self.dataset["fullname"] = env.db.get_property(self._object, "fullname")
        self.dataset["email"] = env.db.get_property(self._object, "email")
        self.dataset["available_timezones"] = localization.get_timezones()
        self.dataset["timezone"] = env.db.get_property(
            self._object, "timezone", default=env.config.general.default_timezone
        )
        self.dataset["available_languages"] = localization.getLanguagesAndIdentifiers()
        self.dataset["language"] = env.db.get_property(
            self._object, "language", default=env.config.general.default_locale
        )
        self.dataset["available_themes"] = theme.getThemes()
        self.dataset["user.theme"] = env.db.get_property(
            self._object, "theme", default=env.config.general.default_theme
        )
Esempio n. 6
0
    def save(self, name):
        user = usergroup.User(name)
        modify_self = env.request.user == user

        if not env.auth.has_user(user):
            raise error.PrewikkaUserError(N_("Invalid user"),
                                          N_("Specified user does not exist"),
                                          log_priority=log.WARNING)

        if name != env.request.user.name and not env.request.user.has(
                "USER_MANAGEMENT"):
            raise error.PrewikkaUserError(
                N_("Permission Denied"),
                N_("Cannot modify other users settings"),
                log_priority=log.WARNING)

        if not env.request.parameters[
                "language"] in localization.get_languages():
            raise error.PrewikkaUserError(
                N_("Invalid Language"),
                N_("Specified language does not exist"),
                log_priority=log.WARNING)

        if not env.request.parameters[
                "timezone"] in localization.get_timezones():
            raise error.PrewikkaUserError(
                N_("Invalid Timezone"),
                N_("Specified timezone does not exist"),
                log_priority=log.WARNING)

        if "password_new" in env.request.parameters and env.request.parameters[
                "password_new"] != env.request.parameters.get(
                    "password_new_confirmation"):
            raise error.PrewikkaUserError(N_("Password error"),
                                          N_("Password mismatch"))

        reload_type = 0

        for param, reload in (("fullname", "none"), ("email", "none"),
                              ("timezone", "view"), ("theme", "window"),
                              ("language", "window")):
            value = env.request.parameters.get(param)
            if value != user.get_property(param):
                if value:
                    user.set_property(param, value)
                else:
                    user.del_property(param)

                if modify_self:
                    reload_type = max(reload_type, ReloadEnum[reload])

        list(hookmanager.trigger("HOOK_USERMANAGEMENT_USER_MODIFY", user))

        if env.request.user.has(
                "USER_MANAGEMENT") and env.auth.can_manage_permissions():
            permissions = self._make_permissions_from_parameters(
                user.permissions)

            if permissions != set(user.permissions):
                old_perms = set(
                    env.auth.get_user_permissions(
                        user, ignore_group=True)).difference(
                            env.auth.get_user_permissions_from_groups(user))
                user.permissions = permissions
                self.log_property_list_change("permissions", user, old_perms,
                                              permissions)
                reload_type = max(
                    ReloadEnum["window"] if modify_self else
                    ReloadEnum[".commonlisting"], reload_type)

            # Group memberships
            if env.auth.can_manage_group_members():
                groups = set(
                    usergroup.Group(i)
                    for i in env.request.parameters.getlist("member_object"))
                if groups != set(env.auth.get_member_of(user)):
                    self.log_property_list_change("groups", user,
                                                  env.auth.get_member_of(user),
                                                  groups)
                    env.auth.set_member_of(user, groups)
                    reload_type = max(
                        ReloadEnum["window"] if modify_self else
                        ReloadEnum[".commonlisting"], reload_type)

        if "password_new" in env.request.parameters:
            if modify_self:
                try:
                    env.auth.authenticate(
                        name,
                        env.request.parameters.get("password_current", ""))
                except auth.AuthError:
                    raise error.PrewikkaUserError(
                        N_("Password error"), N_("Invalid password specified"))

            env.auth.set_password(user, env.request.parameters["password_new"])

        user.sync_properties()

        if reload_type > ReloadEnum["none"]:
            return response.PrewikkaResponse({
                "type": "reload",
                "target": reload_type.name
            })