title: Model login constraints based on time
body: <p>Good morning,</p>
  
  <p>for an existing web application I need to implement "time based login constraints". It means that for each user, later maybe each group, I can define timeslots when they are (not) allowed to log in into the system. As all data for the application is stored in database tables, I need to somehow create a way to model this idea in that way.</p>
  
  <p>My first approach, I will try to explain it here:</p>
  
  <ul>
  <li>Create a tree of login constraints (called "timeslots") with the main "categories", like "workday", "weekend", "public holiday", etc. on the top level, which are in a "sorted" order (meaning "public holiday" has a higher priority than "weekday")</li>
  <li>for each top level node create subnodes, which have a finer timespan, like "monday", "tuesday", ...</li>
  <li>below that, create an "hour" level: 0, 1, 2, ..., 23. No further details are necessary.</li>
  <li>set every member to "allowed" by default</li>
  <li>For every member of the system create a <code>1:n</code> relationship <code>member:timeslots</code> which defines constraints, e.g. a member A may have <code>A:monday-forbidden</code> and <code>A:tuesday-forbidden</code></li>
  <li>Do a depth-first search at every login and check if the member has a constraint.
  Why a depth first search? Well, I thought that it may be that a member has the rules:</li>
  </ul>
  
  <p><code>A:monday-&gt;forbidden</code>, <code>A:monday-10-&gt;allowed</code>, <code>A:mondey-11-&gt;allowed</code></p>
  
  <p>So a login on monday at 12:30 would fail, but one at 10:30 succeed.</p>
  
  <p>For performance reasons I could break the relational database paradigm and set a flag for every entry in the member-to-timeslots-table which is set to true if the member has information set for "finer" timeslots, but that's a second step.</p>
  
  <p>Is this model in principle a good idea? Are there existing models?</p>
  
  <p>Thanks.</p>