librelist archives

« back to archive

bamboo中加入include mixin机制

bamboo中加入include mixin机制

From:
Tang Daogang
Date:
2012-03-21 @ 03:54
现在,可以使用mixin了。

mixin中可以包含:
1. 字段;
2. 方法;
3. init方法;
4. 修饰器。




2012/3/21 Uno Young <littlehaker@gmail.com>

> module(..., package.seeall)
>
>
> local Model = require 'bamboo.model'
> local Session = require 'bamboo.session'
> local md5 = require 'md5'
> local socket = require 'socket'
>
>
> local User = Model:extend {
>     __tag = 'Bamboo.Model.User';
> __name = 'User';
>  __desc = 'Basic user definition.';
> __indexfd = "username";
>  __fields = {
> ['username'] = { required=true, unique=true },
> ['password'] = { required=true },
>  ['salt'] = {},
> ['email'] = { required=true },
> ['nickname'] = {},
>  ['created_date'] = {type="number"},
>
> ['perms'] = { foreign="Permission", st="MANY" },
>  ['groups'] = { foreign="Group", st="MANY" },
> };
>
>
> init = function (self, t)
> if not t then return self end
>
> self.username = t.username
> self.email = t.email
> self.nickname = t.nickname
>  self.created_date = socket.gettime()
>
> math.randomseed(os.time())
>  self.salt = tostring(math.random(1, 1000000))
> -- if t.encrypt and type(t.encrypt) == 'function' then
>  -- self.password = t.encrypt(t.password or '')
> -- else
>  -- self.password = md5.sumhexa(t.password or '')
> -- end
>
> if self.encrypt and type(self.encrypt) == 'function' then
> -- self.password = self.encrypt((t.password or '') .. self.salt)
>       self.password = self:encrypt(t.password)
> end
>
> return self
>  end;
>
> encrypt = function(self, password)
>     return md5.sumhexa(password .. (self.salt or '')):lower()
>   end;
>
> authenticate = function (self, params)
> I_AM_CLASS(self)
>
> local user = self:getByIndex(params.username)
> if not user then return false end
>  -- if md5.sumhexa(params.password):lower() ~= user.password then
> -- if params.password:lower() ~= user.password then
>  if self.encrypt and type(self.encrypt) == 'function' then
> -- if self.encrypt(params.password .. (user.salt or '')):lower() ~=
> user.password then
>  if user:encrypt(params.password) ~= user.password then
> return false
> end
>  else
> if (params.password):lower() ~= user.password then
> return false
>  end
> end
> return true, user
>  end;
>
> login = function (self, params)
> I_AM_CLASS_OR_INSTANCE(self)
>  -- make instance can use this login
> if isInstance(self) then params = self end
>  if not params['username'] or not params['password'] then return nil end
> local authed, user = self:authenticate(params)
>  if not authed then return nil end
>
> Session:setKey('user_id', self:classname() + ':' + user.id)
>  return user
> end;
>
> logout = function (self)
>  -- I_AM_CLASS(self)
> -- Class and instance can both call this function
> return Session:delKey('user_id')
>  end;
>
> register = function (self, params)
> I_AM_CLASS(self)
>  if not params['username'] or not params['password'] then return nil,
> 101, 'less parameters.' end
>
> local user_id = self:getIdByIndex(params.username)
>  if user_id then return nil, 103, 'the same name user exists.' end
>
> local user = self(params)
>  user:save()
>
> return user
> end;
>
> set = function (self, req)
> I_AM_CLASS(self)
> local user_id = req.session['user_id']
>  if user_id then
> req.user = self:getById(user_id)
> else
>  req.user = nil
> end
> return self
>  end;
>
> }
>
> return User
>
>
>
>
>


-- 
Nothing is impossible.