Ejemplo n.º 1
0
    def _get_display(my):

        # set up the security object
        from pyasm.security import Security, Sudo
        from pyasm.biz import Project
        from pyasm.web import WebContainer
        web = WebContainer.get_web()

        security = Security()
        security = my.handle_security(security)
        is_logged_in = security.is_logged_in()


        # guest mode
        #
        allow_guest = Config.get_value("security", "allow_guest")
        if allow_guest == 'true':
            allow_guest = True
        else:
            allow_guest = False

        guest_mode = Config.get_value("security", "guest_mode")
        if not guest_mode:
            guest_mode = 'restricted'

        #allow_guest = True
        #guest_mode = "full"



        # if not logged in, then log in as guest
        if not is_logged_in:
            if not allow_guest:
                return my.handle_not_logged_in()
            else:
                # login as guest
                security = Security()
                my.handle_guest_security(security)


        # for here on, the user is logged in
        login_name = Environment.get_user_name()



        # check if the user has permission to see this project
        project = web.get_context_name()
        if project == 'default':
            override_default = Config.get_value("install", "default_project")
            if override_default:
                project = override_default
        if project != 'default':
            security_version = get_security_version()
            if security_version == 1:
                default = "view"
                access = security.check_access("project", project, "view", default="view")
            else:
                default = "deny"
                key = { "code": project }
                key2 = { "code": "*" }
                #keys = [key]
                keys = [key, key2]
                access = security.check_access("project", keys, "allow", default=default)
        else:
            # you always have access to the default project
            access = True


        access = True
        if not access:
            if login_name == "guest":
                from pyasm.widget import WebLoginWdg

                msg = web.get_form_value(WebLoginWdg.LOGIN_MSG)
                if not msg:
                    msg = "User [%s] is not allowed to see this project [%s]" % (login_name, project)
                    web.set_form_value(WebLoginWdg.LOGIN_MSG, msg)
                return my.handle_not_logged_in(allow_change_admin=False)

            else:
                from pyasm.widget import WebLicenseWdg, BottomWdg, Error403Wdg
                widget = Widget()
                top = my.get_top_wdg()
                widget.add( top )
                widget.add( Error403Wdg() )
                widget.add( BottomWdg() )
                widget.get_display()
     
                return


        if login_name == 'guest' and guest_mode == "full":
            # some extra security for guest users
            guest_url_allow = Config.get_value("security", "guest_url_allow")
            if guest_url_allow:
                items = guest_url_allow.split("|")
                allowed = False
                if my.hash:
                    url = my.hash[0]
                else:
                    url = "index"
                for item in items:
                    item = item.strip("/")
                    if item == url:
                        allowed = True
                        break
                if not allowed:
                    return my.handle_not_logged_in()



        # some extra precautions in guest mode
        if login_name == 'guest' and guest_mode != "full":
            # show a restricted guest mode
            from pyasm.widget import WebLoginWdg, BottomWdg
            from tactic.ui.app import TitleTopWdg

            from pyasm.biz import Project
            from tactic.ui.panel import HashPanelWdg
            web = WebContainer.get_web()

            widget = Widget()
            top = TitleTopWdg()
            widget.add(top)
            body = top.get_body()
            body.add_gradient("background", "background", 5, -20)
            body.add_color("color", "color")

            # get the project from the url because we are still 
            # in the admin project at this stage
            current_project = web.get_context_name()
            try:
                if current_project != "default":
                    project = Project.get_by_code(current_project)
                    assert project
            except Exception, e:
                web_wdg = None
            else:
                if not current_project or current_project == "default":
                    current_project = Config.get_value("install", "default_project")
                if current_project and current_project != "default":
                    Project.set_project(current_project)

                    web_wdg = HashPanelWdg.get_widget_from_hash("/guest", return_none=True)
                    if web_wdg:
                        web_wdg = web_wdg.get_buffer_display()
                        top.add(web_wdg)
                else:
                    web_wdg = None

            if not web_wdg:
                msg = "No widget for Guest defined"
                web.set_form_value(WebLoginWdg.LOGIN_MSG, msg)
                top.add(WebLoginWdg() )


            # create a web app and run it through the pipeline
            web_app = WebApp()
            web_app.get_display(widget)
            return
Ejemplo n.º 2
0
    def get_widget_from_hash(cls, hash, return_none=False, force_no_index=False, kwargs={}):

        from pyasm.web import DivWdg
        if hash.startswith("//"):
            use_top = False
            hash = hash[1:]
        else:
            use_top = True

        import re
        p = re.compile("^/(\w+)")
        m = p.search(hash)
        if not m:
            if return_none:
                return None
            print "Cannot parse hash[%s]" % hash
            return DivWdg("Cannot parse hash [%s]" % hash)
        key = m.groups()[0]
        

        # guest user should never be able to see admin site
        if key != 'login':
            security = Environment.get_security()
            login = security.get_user_name()
            if login == "guest" and key == 'admin':
                from pyasm.widget import WebLoginWdg
                # HACK: if the guest access is full, the the outer form
                # is not defined ... force it in here.  This is because the
                # top used it TopWdg and not TitleTopWdg
                div = DivWdg()
                div.add("<form id='form' name='form' method='post' enctype='multipart/form-data'>\n")
                web_login_wdg = WebLoginWdg().get_buffer_display()
                div.add(web_login_wdg)
                div.add("</form>\n")
                return div





        sobject = cls._get_predefined_url(key, hash)

        # look up the url
        if not sobject:
            search = Search("config/url")
            search.add_filter("url", "/%s/%%"%key, "like")
            search.add_filter("url", "/%s"%key)
            search.add_where("or")
            sobject = search.get_sobject()

        if not sobject:
            if return_none:
                return None
            return DivWdg("No Widget found for hash [%s]" % hash)



        config = sobject.get_value("widget")
        config = config.replace('&','&amp;')

        url = sobject.get_value("url")
        url = url.strip()

        # update the config value with expressions
        options = Common.extract_dict(hash, url)
        for name, value in options.items():
            config = config.replace("{%s}" % name, value)


        xml = Xml()
        xml.read_string(config)


        use_index, use_admin, use_sidebar = cls._get_flags(xml, sobject, force_no_index, kwargs)

        # add the admin bar
        security = Environment.get_security()
        is_admin = security.check_access("builtin", "view_site_admin", "allow")

        if is_admin and use_admin:
            # use admin
            from tactic.ui.app import PageNavContainerWdg
            top = PageNavContainerWdg( hash=hash, use_sidebar=use_sidebar )
            return top.get_buffer_display()

        elif use_index:

            # check if there is an index
            search = Search("config/url")
            search.add_filter("url", "/index")
            index = search.get_sobject()
            # just use admin if no index page is found
            if not index:
                from tactic.ui.app import PageNavContainerWdg
                top = PageNavContainerWdg( hash=hash, use_sidebar=use_sidebar )
                return top.get_buffer_display()
                
            config = index.get_value("widget")
            xml = Xml()
            xml.read_string(config)
            node = xml.get_node("element/display")

            options.update(xml.get_node_values_of_children(node))

            class_name = xml.get_value("element/display/@class")
            if class_name:
                options['class_name'] = class_name

            # this passes the hash value to the index widget
            # which must handle it accordingly
            options['hash'] = hash
            top = cls.build_widget(options)

            return top.get_buffer_display()




        # process the options and then build the widget from the xml


        options = Common.extract_dict(hash, url)
        for name, value in kwargs.items():
            options[name] = value

        node = xml.get_node("element/display")
        options.update(xml.get_node_values_of_children(node))

        class_name = xml.get_value("element/display/@class")
        if class_name:
            options['class_name'] = class_name

        widget = cls.build_widget(options)

        name = hash.lstrip("/")
        name_array = name.split("/")
        if name_array:
            name_end = name_array[-1]
            name_end = name_end.replace("_", " ")
            widget.set_name(name_end)
        else:
            widget.set_name(name)

        return widget
Ejemplo n.º 3
0
                            web_wdg = HashPanelWdg.get_widget_from_hash("/login", return_none=True)
                        except Exception, e:
                            from pyasm.widget import ExceptionMinimalWdg
                            web_wdg = ExceptionMinimalWdg(e)
                            web_wdg.add_style("margin: 50px auto")


                        if web_wdg:
                            web_wdg = web_wdg.get_buffer_display()
                            top.add(web_wdg)
                    else:
                        web_wdg = None

                # display default web login
                if not web_wdg:
                    top.add(WebLoginWdg(allow_change_admin=allow_change_admin) )

            finally:
                # sudo out of scope here
                sudo.exit()


        # create a web app and run it through the pipeline
        web_app = WebApp()
        web_app.get_display(widget)
        return




Ejemplo n.º 4
0
                        if web_wdg:
                            if not isinstance(web_wdg, basestring):
                                web_wdg = web_wdg.get_buffer_display()
                            top.add(web_wdg)
                    else:
                        web_wdg = None

                # display default web login
                if not web_wdg:
                    # get login screen from Site
                    link = "/%s" % "/".join(my.hash)
                    web_wdg = site_obj.get_login_wdg(link)
                    if not web_wdg:
                        # else get the default one
                        web_wdg = WebLoginWdg(
                            allow_change_admin=allow_change_admin)

                    top.add(web_wdg)

            finally:
                # sudo out of scope here
                #sudo.exit()
                pass

        # create a web app and run it through the pipeline
        web_app = WebApp()
        web_app.get_display(widget)
        return

    def _get_display(my):
Ejemplo n.º 5
0
                        web_wdg = site_obj.get_login_wdg()
                        if web_wdg:
                            web_wdg = web_wdg.get_buffer_display()
                            top.add(web_wdg)
                    else:
                        web_wdg = None

                # display default web login
                if not web_wdg:

                    # get login screen from Site
                    web_wdg = site_obj.get_login_wdg()
                    if not web_wdg:
                        # else get the default one
                        web_wdg = WebLoginWdg(allow_change_admin=allow_change_admin)
                    
                    top.add(web_wdg)

            finally:
                # sudo out of scope here
                sudo.exit()


        # create a web app and run it through the pipeline
        web_app = WebApp()
        web_app.get_display(widget)
        return


Ejemplo n.º 6
0
    def handle_not_logged_in(self, allow_change_admin=True):

        site_obj = Site.get()
        site_obj.set_site("default")

        DbResource.clear_cache()

        from pyasm.widget import WebLoginWdg, BottomWdg
        from tactic.ui.app import TitleTopWdg

        from pyasm.biz import Project
        from tactic.ui.panel import HashPanelWdg

        web = WebContainer.get_web()

        widget = Widget()

        top = TitleTopWdg()
        widget.add(top)
        body = top.get_body()
        #body.add_gradient("background", "background", 5, -20)
        body.add_color("background", "background")
        body.add_color("color", "color")

        reset_request = web.get_form_value('reset_request') == 'true'
        if reset_request:
            from tactic.ui.widget import ResetPasswordWdg
            top.add(ResetPasswordWdg())
        else:
            reset_msg = web.get_form_value('reset_msg')
            if reset_msg:
                web.set_form_value(WebLoginWdg.LOGIN_MSG, reset_msg)

            web_wdg = None
            sudo = Sudo()
            try:
                # get the project from the url because we are still
                # in the admin project at this stage
                current_project = web.get_context_name()
                try:
                    if current_project != "default":
                        project = Project.get_by_code(current_project)
                        assert project
                except Exception as e:
                    pass
                else:

                    # custom global site login widget
                    if not current_project or current_project == "default":
                        current_project = Project.get_default_project()
                    if current_project and current_project != "default":
                        try:
                            Project.set_project(current_project)
                        except SecurityException as e:
                            print(e)
                            if 'is not permitted to view project' not in e.__str__(
                            ):
                                raise

                        if not web_wdg:
                            web_wdg = site_obj.get_login_wdg()

                        if web_wdg:
                            if not isinstance(web_wdg, basestring):
                                web_wdg = web_wdg.get_buffer_display()
                            top.add(web_wdg)
                    else:
                        web_wdg = None

                # display default web login
                if not web_wdg:
                    # get login screen from Site
                    link = "/%s" % "/".join(self.hash)
                    web_wdg = site_obj.get_login_wdg(link)
                    if not web_wdg:
                        # else get the default one
                        web_wdg = WebLoginWdg(
                            allow_change_admin=allow_change_admin)

                    top.add(web_wdg)

            finally:
                # sudo out of scope here
                sudo.exit()
                pass

        # create a web app and run it through the pipeline
        web_app = WebApp()
        web_app.get_display(widget)
        return
Ejemplo n.º 7
0
 def get_login_wdg(self):
     from pyasm.widget import WebLoginWdg, BottomWdg
     widget = WebLoginWdg()
     return widget