Example #1
0
def profile_delete(e: TextMessageEventObject, name: str):
    # --- Check profile name
    prof = ProfileManager.get_profile_name(name)
    if not prof:
        return [
            HandledMessageEventText(content=_(
                "Profile with the name `{}` not found.").format(name))
        ]

    # --- Check permission

    profiles = ProfileManager.get_user_profiles(e.channel_model.id,
                                                e.user_model.id)
    user_permissions = ProfileManager.get_permissions(profiles)

    if ProfilePermission.PRF_CED not in user_permissions:
        return [
            HandledMessageEventText(
                content=_("Insufficient permission to delete profile."))
        ]

    deleted = ProfileManager.delete_profile(e.channel_oid, prof.id,
                                            e.user_model.id)
    if deleted:
        return [HandledMessageEventText(content=_("Profile deleted."))]
    else:
        return [
            HandledMessageEventText(content=_("Failed to delete the profile."))
        ]
Example #2
0
    def get(self, request, *args, **kwargs):
        # `kwargs` will be used as `nav_param` so extract channel_oid from `kwargs` instead of creating param.
        channel_oid_str = kwargs.get("channel_oid", "")
        channel_oid = safe_cast(channel_oid_str, ObjectId)
        u_profs = ProfileManager.get_user_profiles(channel_oid, get_root_oid(request))

        if u_profs:
            return render_template(
                self.request, _("Channel Management - {}").format(channel_oid), "account/channel/manage.html", {
                    "user_profiles": u_profs,
                    "perm_sum": sorted(ProfileManager.get_permissions(u_profs), key=lambda x: x.code),
                    "channel_oid": channel_oid
                }, nav_param=kwargs)
        else:
            c_prof = ChannelManager.get_channel_oid(channel_oid)

            if c_prof:
                messages.info(
                    request, _("You are redirected to the channel info page "
                               "because you don't have any connections linked to the channel."),
                    extra_tags="info"
                )

                return redirect(reverse("info.channel", kwargs={"channel_oid": channel_oid}))
            else:
                return WebsiteErrorView.website_error(
                    request, WebsiteError.PROFILE_LINK_NOT_FOUND, {"channel_oid": channel_oid_str})
Example #3
0
    def process_pass(self):
        self._profiles = ProfileManager.get_user_profiles(
            self._channel_oid, self._root_oid)

        if self._profiles:
            self._result = ProfileManager.get_permissions(self._profiles)
Example #4
0
def profile_create_internal(e: TextMessageEventObject, name: str,
                            color: Union[str, Color],
                            permission: Union[str, List[ProfilePermission]],
                            perm_lv: Union[str, PermissionLevel]):
    # --- Check permission

    profiles = ProfileManager.get_user_profiles(e.channel_model.id,
                                                e.user_model.id)
    user_permissions = ProfileManager.get_permissions(profiles)

    if ProfilePermission.PRF_CED not in user_permissions:
        return [
            HandledMessageEventText(
                content=_("Insufficient permission to create profile."))
        ]

    # --- Parse name

    prof_name = _parse_name_(e.channel_oid, name)
    if not prof_name:
        return [
            HandledMessageEventText(content=_(
                "The profile name `{}` is unavailable in this channel. "
                "Please choose a different one.").format(name))
        ]

    # --- Parse color

    color = _parse_color_(color)
    if not color:
        return [
            HandledMessageEventText(content=_(
                "Failed to parse color. Make sure it is in the format of `81D8D0`."
            ))
        ]

    # --- Parse permission level

    perm_lv = _parse_permission_level_(perm_lv)
    if not perm_lv:
        return [
            HandledMessageEventText(content=_(
                "Failed to parse permission level. Make sure it is a valid level or check the documentation."
            ))
        ]

    # --- Parse permission

    msg_on_hold = []
    if isinstance(permission, str):
        max_perm_lv = ProfileManager.get_highest_permission_level(profiles)

        parsed_perm = _parse_permission_(permission, max_perm_lv)
        if parsed_perm.has_skipped:
            msg_on_hold.append(
                HandledMessageEventText(content=_(
                    "Strings below were skipped because it cannot be understood as permission:\n{}"
                ).format("\n".join(parsed_perm.skipped))))
        if parsed_perm.has_invalid:
            msg_on_hold.append(
                HandledMessageEventText(content=_(
                    "Permissions below were not be able to set on the profile "
                    "because your permission level is insufficient:\n{}"
                ).format("\n".join([str(p) for p in parsed_perm.invalid]))))
        if not parsed_perm.has_valid:
            return [
                HandledMessageEventText(
                    content=_("Profile permission parsing failed."))
            ] + msg_on_hold

        permission = parsed_perm.valid

    # --- Process permission

    perm_dict = {
        perm.code_str: perm in permission
        for perm in list(ProfilePermission)
    }

    # --- Create Model

    mdl = ChannelProfileModel(ChannelOid=e.channel_model.id,
                              Name=prof_name,
                              Color=color,
                              Permission=perm_dict,
                              PermissionLevel=perm_lv)
    mdl = ProfileManager.register_new_model(e.user_model.id, mdl)

    if mdl:
        return [
            HandledMessageEventText(content=_("Profile created and attached."))
        ] + msg_on_hold
    else:
        return [
            HandledMessageEventText(content=_("Profile registration failed."))
        ] + msg_on_hold