0

Anyone know how to check similar strings in the if...else statement?

My code below is to check whether a user is in the custom restricted permission group so I can create an indicator in the UI.

if ((myMemberships.indexOf(Sitetitle + "  Restricted  Readers")>=0) {
        oInstance.model.dcn[0]._recordSet[i].isMember=false;

But I want to write an array that will store different string combinations of Sitetitle + " Restricted Readers" such as SiteTitle restricted reader(s), SiteTitleRestrictedReader(s), SiteTitle restrict reader(s). So I can prevent administrators typing the group name wrong when they create the group name, and eventually cause the code fail.

10
  • What do you mean "write array into the if...else statement"? Commented Oct 31, 2016 at 18:05
  • 2
    Sorry, your question is really unclear... I simply can't work out what you're trying to achieve Commented Oct 31, 2016 at 18:06
  • I was thinking to create a variable as array so I can store those variety of strings. Just want to solve this issue, doesn't need to be array. Commented Oct 31, 2016 at 18:08
  • 1
    I know what he's trying to achieve: he wants to check to see any similar value to "restricted readers" is in the array. This way he doesn't have to worry about people having to type a specific string. I'm putting together an answer now. Commented Oct 31, 2016 at 18:08
  • 2
    Your data format is wrong and possibly insecure (as you already know). Restricted readers should be an array of user Ids. It should have normalised name like "memberGroups.restrictedReaders". This should be for internal purposes. For viewing, this group should have name which can be translated to human language. Long story short, you need to refactor. Commented Oct 31, 2016 at 18:09

3 Answers 3

2

I recommend using the Array function Array.some MDN. You would do something like the following:

function isRestrictedReader(member){
   return /Restricted\s+Readers/.test(member); 
   // I'm using a RegExp, but you could use other methods.
   // it just has to return a boolean.
}

if (myMemberships.some(isRestrictedReader)){
   oInstance.model.dcn[0]._recordSet[i].isMember=false;
}
Sign up to request clarification or add additional context in comments.

Comments

1

If you don't want to refactor (more on this later), you can use a regular expression to test whether a string matches the pattern you're looking for.

var regex = new RegExp(Sitetitle + "\s*restrict(?:ed)?\s*reader[s]?", "i");

if ((myMemberships.some(function(text){ return text.match(regex) }) {
        oInstance.model.dcn[0]._recordSet[i].isMember=false;

That regular expression I just threw together based on your examples, but I'm sure somebody more experience with regex can come up with a better one.

As was brought out in one of the comments though, this is a very insecure way to handle this kind of data. You can't rely on human readable strings for crucial data. Especially when it's subject to things like typos, etc etc. You should try a different approach. But if for some reason you can't, this should suffice.

1 Comment

Thanks! This is exactly what I'm looking for! Basically SharePoint handles all the permission and secure the content. Even if the admin create the wrong group name, users will not be able to grant access. This if...else statement just helping me to create the text in the front-end to tell users whether they have access.
1
if(/resTrict/i.test('resTrICted') !== false) {


}

1 Comment

While this code snippet may answer the question, it doesn't provide any context to explain how or why. Consider adding a sentence or two to explain your answer. Also, please use the code formatting syntax to format your code snippet ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.