|
@@ -14,20 +14,12 @@ function temp_org()
|
|
|
policies = {},
|
|
|
processes = {},
|
|
|
requests = {},
|
|
|
- request_count = 0,
|
|
|
members = {},
|
|
|
parent = nil,
|
|
|
children = {}
|
|
|
}
|
|
|
end
|
|
|
|
|
|
-modpol.orgs.request_params = {
|
|
|
- add_org = 1,
|
|
|
- delete = 0,
|
|
|
- add_member = 1,
|
|
|
- remove_member = 1
|
|
|
-}
|
|
|
-
|
|
|
-- ==================================================
|
|
|
-- returns org when given its id or name
|
|
|
function modpol.orgs.get_org(arg)
|
|
@@ -79,10 +71,11 @@ end
|
|
|
function modpol.orgs.reset()
|
|
|
for id, org in ipairs(modpol.orgs.array) do
|
|
|
if id > 1 then
|
|
|
- modpol.orgs.array[id] = nil
|
|
|
+ modpol.orgs.array[id] = "removed"
|
|
|
end
|
|
|
end
|
|
|
|
|
|
+ modpol.ocutil.log('Reset all orgs')
|
|
|
modpol.orgs:record('Resetting all orgs', 'org_reset')
|
|
|
end
|
|
|
|
|
@@ -92,7 +85,7 @@ end
|
|
|
function modpol.orgs.init_instance()
|
|
|
local error_msg
|
|
|
if modpol.orgs.array[1] then
|
|
|
- modpol.ocutil.log('Error: instance has already been initialized')
|
|
|
+ modpol.ocutil.log('Error in orgs.init_instance -> instance has already been initialized')
|
|
|
return false
|
|
|
end
|
|
|
|
|
@@ -105,6 +98,7 @@ function modpol.orgs.init_instance()
|
|
|
-- adding instance to org list
|
|
|
modpol.orgs.array[1] = instance
|
|
|
|
|
|
+ modpol.ocutil.log('Initialized the instance org')
|
|
|
modpol.orgs:record('Initialized the instance org', 'create_instance')
|
|
|
|
|
|
return instance
|
|
@@ -127,15 +121,15 @@ function modpol.orgs:record(msg, entry_type)
|
|
|
if type(msg) == 'string' and not(modpol.ocutil.str_empty(msg)) then
|
|
|
entry.action_msg = msg
|
|
|
else
|
|
|
- modpol.ocutil.log('Error: msg must be a non empty string')
|
|
|
+ modpol.ocutil.log('Error in ' .. self.name .. ':record -> msg must be a non empty string')
|
|
|
return false
|
|
|
end
|
|
|
|
|
|
if type(entry_type) == 'string' and not(modpol.ocutil.str_empty(entry_type)) then
|
|
|
entry.entry_type = entry_type
|
|
|
else
|
|
|
- modpol.ocutil.log('Error: entry_type must be a non empty string')
|
|
|
- print(msg, entry_type)
|
|
|
+ modpol.ocutil.log('Error in ' .. self.name .. ':record -> entry_type must be a non empty string')
|
|
|
+ modpol.ocutil.log(msg, entry_type)
|
|
|
return false
|
|
|
end
|
|
|
|
|
@@ -152,17 +146,17 @@ end
|
|
|
-- ex: instance:add_org('town hall')
|
|
|
function modpol.orgs:add_org(name)
|
|
|
if self.id == nil then
|
|
|
- modpol.ocutil.log('Error: add_org can only be called by another org')
|
|
|
+ modpol.ocutil.log('Error in ' .. self.name .. ':add_org -> add_org can only be called by another org')
|
|
|
return false
|
|
|
end
|
|
|
|
|
|
if modpol.ocutil.str_empty(name) then
|
|
|
- modpol.ocutil.log('Error: org name is required')
|
|
|
+ modpol.ocutil.log('Error in ' .. self.name .. ':add_org -> org name is required')
|
|
|
return false
|
|
|
end
|
|
|
|
|
|
if modpol.orgs.get_org(name) then
|
|
|
- modpol.ocutil.log('Error: org name is already being used')
|
|
|
+ modpol.ocutil.log('Error in ' .. self.name .. ':add_org -> org name is already being used')
|
|
|
return false
|
|
|
end
|
|
|
|
|
@@ -182,7 +176,7 @@ function modpol.orgs:add_org(name)
|
|
|
modpol.orgs.array[child_org.id] = child_org
|
|
|
|
|
|
self:record('created sub org ' .. name, 'add_org')
|
|
|
- modpol.ocutil.log('Created sub org ' .. name)
|
|
|
+ modpol.ocutil.log('Created ' .. name .. ' (suborg of ' .. self.name .. ')')
|
|
|
|
|
|
return child_org
|
|
|
end
|
|
@@ -193,20 +187,20 @@ end
|
|
|
-- note: "reason" param was removed, can be added back
|
|
|
function modpol.orgs:delete()
|
|
|
if self.id == 1 then
|
|
|
- modpol.ocutil.log('Error: cannot delete instance')
|
|
|
+ modpol.ocutil.log('Error in ' .. self.name .. ':delete -> cannot delete instance')
|
|
|
return false
|
|
|
end
|
|
|
|
|
|
if #self.children > 0 then
|
|
|
for i, child_id in pairs(self.children) do
|
|
|
local child = modpol.orgs.get_org(child_id)
|
|
|
- print(child_id, child)
|
|
|
+ modpol.ocutil.log(child_id, child)
|
|
|
child:delete()
|
|
|
end
|
|
|
end
|
|
|
|
|
|
modpol.orgs.array[self.id] = 'removed'
|
|
|
- modpol.ocutil.log('Removed ' .. self.name .. ': ' .. self.id)
|
|
|
+ modpol.ocutil.log('Deleted org ' .. self.name .. ': ' .. self.id)
|
|
|
|
|
|
self:record('Deleted ' .. self.name .. ' and all child orgs', 'del_org')
|
|
|
|
|
@@ -226,6 +220,12 @@ end
|
|
|
-- ===========================================
|
|
|
-- adds a user to an org
|
|
|
function modpol.orgs:add_member(user)
|
|
|
+ for id, name in ipairs(self.members) do
|
|
|
+ if user == name then
|
|
|
+ modpol.ocutil.log('Error in ' .. self.name .. ':add_member -> user already in org')
|
|
|
+ return false
|
|
|
+ end
|
|
|
+ end
|
|
|
-- trys to fill in empty spots first
|
|
|
local empty_index = self:get_member_index('')
|
|
|
if empty_index then
|
|
@@ -234,6 +234,8 @@ function modpol.orgs:add_member(user)
|
|
|
-- adds to end if no empty spots
|
|
|
table.insert(self.members, user)
|
|
|
end
|
|
|
+
|
|
|
+ modpol.ocutil.log('Added member ' .. user .. ' to ' .. self.name)
|
|
|
self:record('Added member ' .. user, 'add_member')
|
|
|
|
|
|
end
|
|
@@ -246,7 +248,10 @@ function modpol.orgs:remove_member(user)
|
|
|
user_index = self:get_member_index(user)
|
|
|
if user_index then
|
|
|
self.members[user_index] = ''
|
|
|
+ else
|
|
|
+ modpol.ocutil.log('Error in ' .. self.name .. ':remove_member -> user not in org')
|
|
|
end
|
|
|
+ modpol.ocutil.log('Removed member ' .. user .. ' from ' .. self.name)
|
|
|
self:record('Removed member ' .. user, 'del_member')
|
|
|
end
|
|
|
|
|
@@ -266,7 +271,8 @@ end
|
|
|
function modpol.orgs:list_member()
|
|
|
local str
|
|
|
for k, v in ipairs(self.members) do
|
|
|
- if str then
|
|
|
+ -- checking to see if member name is valid
|
|
|
+ if str and str ~= '' then
|
|
|
str = str .. '\n' .. v
|
|
|
else
|
|
|
str = v
|
|
@@ -275,56 +281,28 @@ function modpol.orgs:list_member()
|
|
|
return str
|
|
|
end
|
|
|
|
|
|
--- ===========================
|
|
|
--- compares to requests to see if they are identical
|
|
|
-function modpol.orgs.comp_req(req1, req2)
|
|
|
- -- compares request type
|
|
|
- if req1.type ~= req2.type then
|
|
|
- return false
|
|
|
- else
|
|
|
- -- comparing parameters
|
|
|
- -- we can assume the number of params is the same as this is checked in the make_request func
|
|
|
- for k, v in ipairs(req1.params) do
|
|
|
- if v ~= req2.params[k] then
|
|
|
- return false
|
|
|
- end
|
|
|
+-- ==============================
|
|
|
+-- because member list uses lazy deletion, using #org.members will not show an accurate number
|
|
|
+function modpol.orgs:get_member_count()
|
|
|
+ local count = 0
|
|
|
+ for k, v in ipairs(self.members) do
|
|
|
+ -- the empty string represents a deleted member in the members list
|
|
|
+ if v ~= '' then
|
|
|
+ count = count + 1
|
|
|
end
|
|
|
end
|
|
|
- return true
|
|
|
+ return count
|
|
|
+end
|
|
|
+-- ====================================
|
|
|
+-- adds a new policy to the policy table
|
|
|
+-- must define the policy type, process associated with it, and whether the request must be made by an org member
|
|
|
+function modpol.orgs:set_policy(policy_type, process_type, must_be_member)
|
|
|
+ local new_policy = {
|
|
|
+ process_type = process_type,
|
|
|
+ must_be_member = must_be_member
|
|
|
+ }
|
|
|
+ self.policies[policy_type] = new_policy
|
|
|
+ modpol.ocutil.log('Added policy for ' .. policy_type .. ' in ' .. self.name)
|
|
|
end
|
|
|
|
|
|
--- ================================
|
|
|
--- tries to make a request to the org
|
|
|
-function modpol.orgs:make_request(request)
|
|
|
- -- makes sure the request has the valid number of parameters
|
|
|
- local num_params = modpol.orgs.request_params[request.type]
|
|
|
-
|
|
|
- if num_params == nil then
|
|
|
- modpol.ocutil.log("Error: request type is invalid")
|
|
|
- return false
|
|
|
- end
|
|
|
-
|
|
|
- for k, v in ipairs(request.params) do
|
|
|
- num_params = num_params - 1
|
|
|
- end
|
|
|
-
|
|
|
- if num_params ~= 0 then
|
|
|
- modpol.ocutil.log("Error: request has invalid number of parameters")
|
|
|
- return false
|
|
|
- end
|
|
|
-
|
|
|
- -- checking to see if identical request already exists
|
|
|
- for k, v in ipairs(self.requests) do
|
|
|
- if self.comp_req(request, v) == true then
|
|
|
- modpol.ocutil.log("Error: request has already been made")
|
|
|
- return false
|
|
|
- end
|
|
|
- end
|
|
|
-
|
|
|
- -- use lazy deletion here, not very clean
|
|
|
- -- table.insert(self.requests, request)
|
|
|
- self.request_count = self.request_count + 1
|
|
|
- self.requests[self.request_count] = request
|
|
|
- return self.request_count
|
|
|
|
|
|
-end
|