/** * Dump YAML from PHP array statically * * The dump method, when supplied with an array, will do its best * to convert the array into friendly YAML. Pretty simple. Feel free to * save the returned string as nothing.yaml and pass it around. * * Oh, and you can decide how big the indent is and what the wordwrap * for folding is. Pretty cool -- just pass in 'false' for either if * you want to use the default. * * Indent's default is 2 spaces, wordwrap's default is 40 characters. And * you can turn off wordwrap by passing in 0. * * @access public * @return string * @param array $array PHP array * @param int $indent Pass in false to use the default, which is 2 * @param int $wordwrap Pass in 0 for no wordwrap, false for default (40) */ function YAMLDump($array, $indent = false, $wordwrap = false) { $spyc = new Helper_Spyc(); return $spyc->dump($array, $indent, $wordwrap); }
/** * 生成ACL文件 权限列表 */ function actionMakeAclFile() { $this->_pathway->addStep('更新权限文件'); //指定管理员角色或者最高权限角色 $root_role = 'ADMIN'; //取得数据表中的所有权限节点的数据 //这种方法不能取得相关的用户角色的数据,直接生成数组 $nca = Permissions::find()->asArray()->getAll(); $nca = Permissions::find()->getAll(); // 将取得的数据数组进行重新构建 // ACL_NULL ACL_EVERYONE ACL_HAS_ROLE ACL_NO_ROLE $acl_arr = array(); //默认命名空间允许管理员访问,注意要小写 $acl_arr['all_controllers']['allow'] = '"' . $root_role . '"'; foreach ($nca as $value) { $value['namespace'] = strtolower($value['namespace']); $value['controller'] = strtolower($value['controller']); $value['action'] = strtolower($value['action']); $roles_arr = $value->roles->toArray(); if ($value['namespace'] != 'default') { $acl_arr[$value['namespace']]['all_controllers']['allow'] = '"' . $root_role . '"'; } if ($value['rbac'] != 'ACL_NULL') { if ($value['namespace'] == 'default') { $acl_arr[$value['controller']]['actions'][$value['action']]['allow'] = $value['rbac']; } else { $acl_arr[$value['namespace']][$value['controller']]['actions'][$value['action']]['allow'] = $value['rbac']; } } elseif (!empty($roles_arr)) { $roles = array(); foreach ($roles_arr as $value2) { if ($value2['name'] != $root_role) { $roles[] = $value2['name']; } } $roles = implode(',', Q::normalize($roles)); if ($value['namespace'] == 'default') { $acl_arr[$value['controller']]['actions']['all_actions']['allow'] = '"' . $root_role . '"'; $acl_arr[$value['controller']]['actions'][$value['action']]['allow'] = '"' . $root_role . ',' . $roles . '"'; } else { $acl_arr[$value['namespace']][$value['controller']]['actions']['all_actions']['allow'] = '"' . $root_role . '"'; $acl_arr[$value['namespace']][$value['controller']]['actions'][$value['action']]['allow'] = '"' . $root_role . ',' . $roles . '"'; } } } // 将数组转换成YAML格式,自定义了一个yamldump的方法 $Helper_Spyc = new Helper_Spyc(); $acl_yaml = <<<EOT # <?php die(); ?> ## 注意:书写时,缩进不能使用 Tab,必须使用空格 ############################# # 访问规则 ############################# # ACL_EVERYONE : 任何用户 # ACL_HAS_ROLE : 具有任何角色的用户 # ACL_NO_ROLE : 不具有任何角色的用户 # ACL_NULL : 没有系统角色 EOT; $acl_yaml .= $Helper_Spyc->YAMLDump($acl_arr); //框架生成的方法,不过不能声场YAML格式,而是jeson格式 //$acl_yaml2 = Helper_YAML::dump($acl_arr); //取得acl文件物理路径 $acl_filename = rtrim(dirname(__FILE__), '/\\') . DS . '..' . DS . '..' . DS . '..' . DS . 'config' . DS . 'acl.yaml'; //写入方式打开写入文件的路径 $fp = fopen($acl_filename, "w+"); //判断是否生成了文件,并返回结果 if (fwrite($fp, $acl_yaml)) { fclose($fp); return '更新权限文件成功!'; } else { fclose($fp); return '更新权限文件失败!'; } }