小编Joe*_*ams的帖子

在PostgreSQL中使用视图进行访问控制

我有一个表格的模式,其内容基本上归结为:

  • 一组用户
  • 一组对象组
  • 访问控制列表(acl),指示用户可以访问哪些组
  • 一组对象,每个对象只属于一个组.

我想创建一个支持访问控制的简单应用程序.我认为这里的观点会很好.

假设我有以下数据库初始化:

/* Database definition */

BEGIN;

CREATE SCHEMA foo;

CREATE TABLE foo.users (
    id SERIAL PRIMARY KEY,
    name TEXT
);

CREATE TABLE foo.groups (
    id SERIAL PRIMARY KEY,
    name TEXT
);

CREATE TABLE foo.acl (
    user_ INT REFERENCES foo.users,
    group_ INT REFERENCES foo.groups
);

CREATE TABLE foo.objects (
    id SERIAL PRIMARY KEY,
    group_ INT REFERENCES foo.groups,
    name TEXT,
    data TEXT
);

/* Sample data */

-- Create groups A and B
INSERT INTO foo.groups …
Run Code Online (Sandbox Code Playgroud)

postgresql access-control sql-view

5
推荐指数
1
解决办法
1760
查看次数

如何使用外部PHP脚本获取当前Joomla用户

我有一些用于AJAX查询的PHP脚本,但我希望它们能够在Joomla的身份验证系统的保护下运行.以下是安全的吗?有没有不必要的线路?

joomla-auth.php(与Joomla的index.php位于同一目录中):

<?php

define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

/* Create the Application */
$mainframe =& JFactory::getApplication('site');

/* Make sure we are logged in at all. */
if (JFactory::getUser()->id == 0)
    die("Access denied: login required.");

?>
Run Code Online (Sandbox Code Playgroud)

test.php的:

<?php

include 'joomla-auth.php';

echo 'Logged in as "' . JFactory::getUser()->username . '"';

/* We then proceed to access things only the user
   of that name has access to. */ …
Run Code Online (Sandbox Code Playgroud)

php authentication joomla external-script

5
推荐指数
1
解决办法
2万
查看次数

Shift-reduce:何时停止减少?

我正在尝试学习shift-reduce解析.假设我们有以下语法,使用强制执行操作顺序的递归规则,受ANSI C Yacc语法的启发:

S: A;

P
    : NUMBER
    | '(' S ')'
    ;

M
    : P
    | M '*' P
    | M '/' P
    ;

A
    : M
    | A '+' M
    | A '-' M
    ;
Run Code Online (Sandbox Code Playgroud)

我们想要使用shift-reduce解析来解析1 + 2.首先,1被移动为NUMBER.我的问题是,它是否减少到P,然后是M,然后是A,然后是S?它是如何知道停在哪里的?

假设它确实一直减少到S,然后转移'+'.我们现在有一个堆栈包含:

S '+'
Run Code Online (Sandbox Code Playgroud)

如果我们转移'2',减少可能是:

S '+' NUMBER
S '+' P
S '+' M
S '+' A
S '+' S
Run Code Online (Sandbox Code Playgroud)

现在,在最后一行的任一侧,S可以是P,M,A或NUMBER,并且在任何组合都是文本的正确表示的意义上它仍然有效.解析器如何"知道"来实现它

A '+' M
Run Code Online (Sandbox Code Playgroud)

这样它可以将整个表达式减少到A,那么S?换句话说,在转移下一个令牌之前,它如何知道停止减少?这是LR解析器生成的关键难点吗?


编辑:问题的补充如下.

现在假设我们解析1+2*3.一些转移/减少操作如下:

Stack    | Input | Operation
---------+-------+----------------------------------------------
         | 1+2*3 …
Run Code Online (Sandbox Code Playgroud)

theory parsing context-free-grammar formal-languages shift-reduce

5
推荐指数
1
解决办法
328
查看次数

JavaScript中的基本流控制

请您解释一下,如何在JavaScript中编写真正基本的流控制?谢谢.

flow([

  function(callback) { /* do something */ callback(); /* run next function */ },
  function(callback) { /* do something */ callback(); /* run next function */ },
  function(callback) { /* do something */ callback(); /* run next function */ },
  function(callback) { /* do something */ callback();  }

], function() {

  alert("Done.");

});
Run Code Online (Sandbox Code Playgroud)

javascript

5
推荐指数
2
解决办法
1828
查看次数

在C++中异步下载HTTP URL

在Linux上用C++ 下载HTTP URL(例如http://0.0.0.0/foo.htm)的好方法是什么?我非常喜欢异步的东西.我的程序将有一个事件循环,它会重复启动多个(非常小的)下载并在它们完成时对它们进行操作(通过轮询或以某种方式通知).我宁愿不必生成多个线程/进程来完成此任务.这不应该是必要的.

我应该查看像libcurl这样的库吗?我想我可以使用非阻塞TCP套接字和select()调用手动实现它,但这可能不太方便.

c++ asynchronous http download

4
推荐指数
2
解决办法
4971
查看次数

SQL选择一行的后代

假设在SQL中实现树结构,如下所示:

CREATE TABLE nodes (
    id INTEGER PRIMARY KEY,
    parent INTEGER -- references nodes(id)
);
Run Code Online (Sandbox Code Playgroud)

尽管可以在此表示中创建循环,但我们假设我们永远不会让这种情况发生.该表只存储一个根集合(父节点为null的记录)及其后代.

目标是,给定表中节点的id,找到作为其后代的所有节点.

是的后代如果任一个的父是的父是的后代.注意递归定义.

以下是一些示例数据:

INSERT INTO nodes VALUES (1, NULL);
INSERT INTO nodes VALUES (2, 1);
INSERT INTO nodes VALUES (3, 2);
INSERT INTO nodes VALUES (4, 3);
INSERT INTO nodes VALUES (5, 3);
INSERT INTO nodes VALUES (6, 2);
Run Code Online (Sandbox Code Playgroud)

代表:

1
`-- 2
    |-- 3
    |   |-- 4
    |   `-- 5
    |
    `-- …
Run Code Online (Sandbox Code Playgroud)

sql sqlite recursion recursive-query

4
推荐指数
2
解决办法
1676
查看次数

如何推送(即刷新)发送到TCP流的数据

RFC 793说TCP定义了一个"推送"功能,确保接收者获得数据:

有时用户需要确保已经传输了他们提交给TCP的所有数据.为此,定义了推送功能.为了确保实际传输提交给TCP的数据,发送用户指示应该将其推送到接收用户.推送使TCP迅速转发并将数据传送到接收器.

但是,我找不到push系统调用.使用fsync文件描述符产生一个无效参数错误.

我用一个简单的服务器进行了实验,该服务器接受来自客户端的连接,等待,然后向客户端发送26个字节:

#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>

#define PORT 1234

int main(void)
{
    int server_fd;
    int client_fd;

    if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket");
        return 1;
    }

    {
        struct sockaddr_in addr;
        memset(&addr, 0, sizeof(addr));
        addr.sin_family = AF_INET;
        addr.sin_port = htons(PORT);
        addr.sin_addr.s_addr = INADDR_ANY;

        if (bind(server_fd, (struct sockaddr*)&addr, sizeof(addr)) != 0) {
            perror("bind");
            return 1;
        }
    }

    if (listen(server_fd, 20) != 0) { …
Run Code Online (Sandbox Code Playgroud)

c unix sockets tcp

4
推荐指数
1
解决办法
7620
查看次数

将任何Literate Haskell渲染为PDF,HTML或类似内容

如何将Literate Haskell文件转换为更容易的眼睛?这是当我尝试做一件明显的事情时会发生什么:

$ latex Check.lhs
This is pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009/Debian)
entering extended mode
(./Check.lhs
LaTeX2e <2009/09/24>
Babel <v3.8l> and hyphenation patterns for english, usenglishmax, dumylang, noh
yphenation, loaded.

! LaTeX Error: Environment code undefined.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.7 \begin{code}
Run Code Online (Sandbox Code Playgroud)

我不熟悉处理tex文件的常用命令.我可以使用什么简单,严肃的方式将任何.lhs文件转换为PDF或HTML?

latex haskell

4
推荐指数
2
解决办法
2774
查看次数

Haskell:`show`的变体,它不会在引号中包装String和Char

我想的变体show(我们称之为label),其行为就像show,只是它不换行StringS IN " "CharS IN ' '.例子:

> label 5
"5"
> label "hello"
"hello"
> label 'c'
"c"
Run Code Online (Sandbox Code Playgroud)

我尝试手动实现这个,但我碰到了一些墙.这是我尝试过的:

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
module Label where

class (Show a) => Label a where
    label :: a -> String

instance Label [Char] where
    label str = str

instance Label Char where
    label c = [c]

-- Default case
instance Show a => Label a …
Run Code Online (Sandbox Code Playgroud)

haskell pretty-print

3
推荐指数
1
解决办法
2323
查看次数

在同一查询中使用列参数化set-returns函数

基本上,我想要做的是:

SELECT set_returning_func(id) FROM my_table;
Run Code Online (Sandbox Code Playgroud)

但是,结果将是记录语法中的单个列,例如

             set_returning_func              
---------------------------------------------
 (old,17,"August    2, 2011 at 02:54:59 PM")
 (old,28,"August    4, 2011 at 08:03:12 AM")
(2 rows)
Run Code Online (Sandbox Code Playgroud)

我希望将它解压缩到列中.如果我这样写这样的查询:

SELECT srf.* FROM my_table, set_returning_func(my_table.id);
Run Code Online (Sandbox Code Playgroud)

我收到一条错误消息:

ERROR:  function expression in FROM cannot refer to other relations of same query level
Run Code Online (Sandbox Code Playgroud)

那么,如何获得结果集,同时还提供带参数的set-returns函数?

postgresql

2
推荐指数
1
解决办法
603
查看次数

虚函数和pthread_create之间的竞争

当我尝试使用虚方法创建一个类实例并将其传递给pthread_create时,我得到一个竞争条件,导致调用者有时会调用基本方法而不是像它应该的那样调用派生方法.谷歌搜索后pthread vtable race,我发现这是一个相当着名的行为.我的问题是,什么是解决它的好方法?

以下代码在任何优化设置中都表现出此行为.请注意,MyThread对象在传递给pthread_create之前已完全构造.

#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Thread {
    pthread_t thread;

    void start() {
        int s = pthread_create(&thread, NULL, callback, this);
        if (s) {
            fprintf(stderr, "pthread_create: %s\n", strerror(errno));
            exit(EXIT_FAILURE);
        }
    }
    static void *callback(void *ctx) {
        Thread *thread = static_cast<Thread*> (ctx);
        thread->routine();
        return NULL;
    }
    ~Thread() {
        pthread_join(thread, NULL);
    }

    virtual void routine() {
        puts("Base");
    }
};

struct MyThread : public Thread {
    virtual void routine() {

    }
};

int …
Run Code Online (Sandbox Code Playgroud)

c++ pthreads race-condition

1
推荐指数
1
解决办法
962
查看次数

如何将字节块读入struct

我有这个资源文件,我需要处理,包含一组文件.

首先,资源文件列出了其中包含的所有文件,以及其他一些数据,例如在此结构中:

struct FileEntry{
     byte Value1;
     char Filename[12];
     byte Value2;
     byte FileOffset[3];
     float whatever;
}
Run Code Online (Sandbox Code Playgroud)

所以我需要读取这个大小的块.

我正在使用FileStream中的Read函数,但是如何指定struct的大小?我用了:

int sizeToRead = Marshal.SizeOf(typeof(Header));
Run Code Online (Sandbox Code Playgroud)

然后将此值传递给Read,但之后我只能读取一组byte [],我不知道如何转换为指定的值(我知道如何获取单字节值...但不是其余的).

另外我需要指定一个不安全的上下文,我不知道它是否正确......

在我看来,读取字节流比我在.NET中认为的更难:)

谢谢!

.net c# struct filestream

1
推荐指数
2
解决办法
9508
查看次数

初始化const变量

我们可以如下初始化const变量吗?

int var1 = 10;
const int var2 = var1;
Run Code Online (Sandbox Code Playgroud)

这会导致任何编译器出现任何警告/错误吗?

c

-1
推荐指数
1
解决办法
6862
查看次数