damonky.co.uk - Web development | Illustration | Flex | Papervision | Actionscript | Devon

Conditional Authentication using .htaccess

Working on sites very close to live is always tricky. Clients want to use the site unhindered, you want to check it through before it goes live. The one thing you don’t want happen is for the public to discover and access the site before it goes live.

The code below allows only grant access without authentication to a set of IP’s / domains. Any request from a  domain not in this list will be asked to authenticate. Satisfy any allows the .htaccess to grant permission to anyone who meets the two key criteria. In the example you will note that google’s domains have been allowed through so as not to affect SEO too much. Replace 123.123.123.123 with your IP, or that of whoever you wish to access the site. I am yet however to find one that works for dynamic IP’s – wouldn’t that be neat :D

You will of course need to create a .htpasswd file. a good generator can be found at http://www.htaccesstools.com/htpasswd-generator/ along with a load of other goodies.


 Bash | 
 copy code |
?
01
#require authentication of requests
02
AuthName "Restricted Area"
03
AuthUserFile /var/www/.htpasswd
04
AuthType basic
05
Require valid-user
06
#deny access to all, allow for selected ip's / domains
07
Order Deny,Allow
08
Deny from all
09
Allow from 123.123.123.123 googlebot.com google.com google-analytics.com
10
#if any of these rules are satisfied they may proceed
11
Satisfy Any
12

Getting that Developer Job

going-for-an-interviewIn the last few weeks I’ve had to interview for three new developer positions, going through the usual interview process.

Developers, Designers, Multimedia; All these roles demand a specialist knowledge. It’s not a job you can just walk in off the street and do. You will have worked to gain those skills that the company desire.

So how can you demonstrate those skills effectively in such a short time? After finding 3 suitable candidates I thought I’d write a few notes about how I what i’m looking for. So here it goes:

1. Research the company


Get to know how the company works. What types of work do they typically do and who do they do it for. Have there been any recent websites that they have done that are notable.

If they have a portfolio study it. It will tell you about what technologies they use, their style, and even approach.You can then use this to see how you could fit into the company. This in turn allows you to prepare your portfolio and questions to be targeted to what they may well be looking for.

2. Dress for the Occasion


Nothing really says I want this job less than your most comfy cardy and faded jeans. There’s a time for being fashionable and this isn’t it. Consider it like a first date with the hotest girl / boy at school. Would you wanna turn up looking rough? I thought not. Demonstrate that you want the job, dress to impress.

3. Prepare your showcase and make it relevant


It was so surprising just how many people came to interview with nothing to show at all. Demonstrate your interest and knowledge of the field. Take in sample of work,personal projects, discuss articles you’ve read about new technologies.

One example is a developer who came in with a Ajax multi-player game of connect 4. Now some would say that this isn’t amazing, there are tons of games like that out there. It wasn’t the game that I found impressive. He briefly introduced the game before proceeding to talk about the technologies he used, why he chose to use them, and how he aimed to improve it.

The candidate had not tried to wow his audience with a polished product, although this would also be cool. He had shown me that he could solve a problem, evaluate his work and consider the ways in which he was looking to improve. What this means for me is someone I can give a problem to rather than instruct a solution, freeing my time to do other things.

4. Stay focused


Sure you can talk about opinions on the web about what the latest technology is and how [ insert technology here ] is like soooo yesterday. The question is what can I do with the technology? what is exciting about it? How could I use it to A) Improve the end product B) Save time C) Enrich my life forever. How have you used it, why did you choose it?

One way of always having something to say is to read blogs. I don’t want what smashing magazine or Boagworld said last week back to me. You are a well studied person. evaluate the text, give a counter point show that you not only read the stuff, but actually understand it.

5. Don’t Panic


Despite what your stomach will be telling you, the people are there to see you. They have stopped everything they were doing to give you the interview. In a studio these times can be rare – especially if they are interviewing because they need a new person. Everyone who interviews you is looking for reasons FOR hiring you, not against. So listen and take your time to respond after considering your answer.

6. Get Feedback


Even if it doesn’t end up being the job for you get feedback. This will help you improve your technique, and prepare for the next .

Query conditions on related tables in CakePHP

Today I came across an interesting problem. When using the recursive features of CakePHP to return a results tree it isn’t possible to refine the results by a condition on the related table when it is a one to many relationship. Take the follow example:

I have 4 tables:
 MySQL | 
 copy code |
?
    User
    -------------------
    id
    username
    password
    country_id
 
    Template
    --------------------
    id
    template
    code
    user_id
 
    TemplateCountry
    ---------------------
    id
    country_id
    template_id
 
    Country
    --------------------
    id
    country
 
    Templates have many TemplateCountries
    Countries  have many TemplateCountries
    Users have many Templates
    TemplateCountries belongs to Templates
    TemplateCountries belongs to Countries 
For the purpose of my experiment I am only retrieving the TemplateCountry entries directly related to the template, for a list in a Flex application I am building.

Now, I wish to return all of the templates and other associated records for a certain country / user. I expected to be able to use something along the lines of:

 MySQL | 
 copy code |
?
$this->Templates->find('all',array('conditions'=>array("Template.user_id" =>$user_id, "TemplateCountry.id" => $country_id))); 

Unfortunately this does not work. This is because when the query is built by the cakePHP engine several queries are built to return the nested tree of results. The first query is simply:
 MySQL | 
 copy code |
?
SELECT * from template as Templates where Template.user_id = $user_id AND TemplateCountry.id = $country_id;
This raises an error because the TemplateCountry table is not part of the initial query. My solution, all be it a cumborsome one, is to then take the result and apply a filter, much like is possible with arraycollections in Actionscript.
 PHP | 
 copy code |
?
$result = $this->Templates->find('all',array('conditions'=>array("Template.user_id" =>$user_id));
$result = $this->filterTemplates($result, $country_id);
----------------------------------
 
function filterTemplates($tpl,$country_id) {
 
        //declare the return as an empty array
        $out = array();
 
        //loop through the returns
        foreach( $tpl as $item) {
 
                //check each country the template is associated to
                foreach( $item['TemplateCountry'] as $country) {
 
                    //if it is the same country push to the new list
                    if($country['country_id'] == $country_id) {
 
                        array_push($out, $item);  
 
                    }
 
                }
 
        }
        //$tpl = $out;
        return $out;
}

This prunes every record which doesn't match my condition from the passed array and then replaces the array in the results tree. It's not the perfect solution, but it does the job. If anyone has any other means of doing this I would love to hear from you.