Created
May 14, 2015 08:38
-
-
Save gebv/01623d7ead3f58a2e0c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package contractor | |
import ( | |
"utils/logger" | |
"utils/crypt" | |
helpfullObject "helpfull/objects" | |
"app/components/contractor/licenses" | |
"app/models" | |
"app/events" | |
"app" | |
) | |
type Session struct { | |
ID string | |
Options helpfullObject.FreeFields | |
User *models.SessionUser | |
// hook | |
OnFlushSession func(string) `json:"-"` | |
} | |
func (s *Session) GenSession() { | |
s.ID = crypt.GenRandomEncodeHex(crypt.GenSalt(nil)) | |
if s.OnFlushSession != nil{ | |
s.OnFlushSession(s.ID) | |
} | |
} | |
// Reset to guest | |
func (s *Session) ResetAsGuest() bool { | |
s.Logout() | |
s.GenSession() | |
s.User = &models.SessionUser{} | |
s.User.Licenses.Add(licenses.Guest) | |
s.Options = helpfullObject.NewFreeFields() | |
return s.Flush() | |
} | |
func (s *Session) SetSessionId(sid string) { | |
s.ID = sid | |
if s.OnFlushSession != nil{ | |
s.OnFlushSession(s.ID) | |
} | |
} | |
func (s *Session) Load() bool { | |
if s._load() { | |
app.Manager.Execute("EV:dispatch", events.SESSION_LOAD, &EventSession{ID: s.ID}) | |
app.Manager.Execute("EV:dispatch", events.SESSION_UPDATED, &EventSession{ID: s.ID}) | |
if s.OnFlushSession != nil { | |
s.OnFlushSession(s.ID) | |
} | |
return true | |
} | |
app.Manager.Execute("EV:dispatch", events.SESSION_NOT_FOUND, &EventSession{ID: s.ID}) | |
return false | |
} | |
func (s *Session) Flush() bool { | |
app.Manager.Execute("EV:dispatch", events.SESSION_FLUSH, &EventSession{ID: s.ID}) | |
return s._flush() | |
} | |
func (s *Session) Logout() bool { | |
app.Manager.Execute("EV:dispatch", events.SESSION_LOGOUT, &EventSession{ID: s.ID}) | |
return s._remove(); | |
} | |
func (s *Session) ExecuteAsGuest(command string, dto ...interface{}) (interface{}, error) { | |
contract, err := s.getContractByLicense(licenses.Guest) | |
if logger.CheckError(err) { | |
return nil, logger.NewError("Contract not found") | |
} | |
return contract.Execute(command, dto...) | |
} | |
func (s *Session) ExecuteAsUser(command string, dto ...interface{}) (interface{}, error) { | |
contract, err := s.getContractByLicense(licenses.User) | |
if logger.CheckError(err) { | |
return nil, logger.NewError("Contract not found") | |
} | |
return contract.Execute(command, dto...) | |
} | |
// Initialization of the contract by license | |
func (s *Session) getContractByLicense(licenseName licenses.LicenseName) (*Contract, error) { | |
contract, err := app.Manager.Execute("contractor:Contract", s, licenseName) | |
if logger.CheckError(err) { | |
return nil, logger.NewError("Error initializing contract") | |
} | |
return contract.(*Contract), nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment