F3 框架:hive 变量的范围和用法

F3 Framework: scope and usage of hive variables

我已经使用 Fat Free Framework 3.6 一段时间了,但我在确定与所用变量相关的一些事情时遇到了一些麻烦;另请注意,作为 PHP 程序员,我的知识并不丰富。 以下是一些示例(我为 SQL 映射器使用了一些 "shortcut" 方法,但我想它仍然可读):

function testroute() {

    // Q1 - Using f3-access to authorize a logged in user to advance on a route
    $this->access->authorize($this->f3->get('SESSION.user.group_id'));


    // Q2 - Change the f3 UPLOADS variable
    $this->f3->update('UPLOADS', '/different/location');


    // Q3 - Instantiante Users and User Groups from DB
    $users = new User($this->db);
    $userGroups = new UserGroups($this->db);

    // Load all records to array
    $arrayOfUsers = $users->all();
    $arrayOfUserGroups = $userGroups->all();

    // Make the arrays available to the template
    $this->f3->set('arrayOfUsers', $arrayOfUsers );
    $this->f3->set('arrayOfUserGroups', $arrayOfUserGroups );

    // Render the View
    $this->f3->set('view','content.test.htm');
    $template=\Template::instance();
    echo $template->render('layout.sidebar.htm');
}

考虑上面的示例代码:

  1. SESSION.user.group_id 变量是否可以被登录用户篡改with/exploited,从而更改为其用户组存储的值?如果是这样,什么是 the/a 更安全的方式来做这样的事情,比如在登录时设置 isAdmin 标志?
  2. 更改 UPLOADS 变量是否会使整个配置单元(即所有用户)都不同,还是仅针对当前用户进行更改?
  3. 在模板中,有没有办法使用给定 usergroup_id 值来获取相关 userGroup 的不同键,例如是蛞蝓?在下面的示例中,我试图避免遍历 @arrayOfGroups,为此我尝试使用 array_search,但它 returns 为空(实际上它 returns slug 对于 id=0),即:

    <include href="{{ 'navbar.htm' }} />

    <repeat group="{{ @arrayOfUsers }}" value="{{ @item }}" >

    <tr class="">
        <td>{{ @item.username }}</td>
        <td>{{ @item.usergroup_id }}</td>
        <td>{{ @arrayOfUserGroups[array_search(@item.usergroup_id].slug }}</td>
    </tr>
    

    </repeat>

  4. 在最后一个示例中,我有一个导航栏的 <include> 引用,而导航栏又将有 <li></li> 个导航项元素。使用此 testroute() 控制器,将 <li class="active"></li> 应用于特定项目的正确方法是什么?

干杯

问题 #1:登录用户可以更改其组吗?

不,用户不能直接修改 SESSION 的内容(除非您已经为他提供了这样做的方法)。唯一可以被利用的是访问本身,如果会话 ID 被盗(又名 "session hijacking" 参见 here or there)。

现在,为了灵活起见,您最好在 SESSION 中保存最低限度。在会话中存储用户组可以防止您动态更改登录用户的组(更改将在下次登录时生效)。我宁愿建议只存储用户 ID 并从中检索组。

问题 #2:更改 UPLOADS 变量是否会使其对整个配置单元(即所有用户)有所不同,还是仅对当前用户进行更改?

仅限当前用户。

注意:整个蜂巢是 "only for the current user"。仅共享缓存​​变量。

问题 #3:如何从 $arrayOfUserGroups 中检索特定组?

$arrayOfUserGroups 是从 $userGroups->all() 计算得出的,我猜这是 DB\SQL\Mapper->find() 方法的结果。该方法不按 id 索引结果,仅按 SQL 输出中的出现顺序索引。

因此,解决问题的一种方法是在返回结果之前重新索引结果。类似于:

function all() {
  $groups=$this->mapper->find('');
  $all=[];
  foreach ($groups as $group)
    $all[$group->id]=$group;
  return $all;
  // or if you prefer a one-liner:
  return array_combine(array_map(function($g){return $g->id;},$groups),$groups);
}

现在在您的模板中:

{{ @arrayOfUserGroups[@item.usergroup_id].slug }}

问题 #4:如何激活导航项?

有多种方法可以实现这一点,这取决于几个因素,例如导航栏层次结构深度、您的路由结构(URL 或别名、静态或动态路由的命名约定)等。

所以我假设您使用的是具有一级层次结构、没有别名且没有 dynamic route. In that case, you could hold the list of nav items (paths+labels) in one variable and compare them with the current PATH 的基本导航栏。例如:

<repeat group="@navItems" value="@item">
  <li class="{{ @item.path==@PATH ? 'active' : '' }}">
    <a href="{{ @item.path }}">{{ @item.label }}</a>
  </li>
</repeat>